2017-06-14 73 views
0

我在LibGDX中創建了一個遊戲。我使用了Pixel Dungeon中使用的瓷磚,並使用Tiled創建了瓷磚地圖。在瓷磚地圖上顯示雪碧libgdx

主要角色的類是Actor一個子類,因爲性格的動畫,我使用這個代碼來繪製精靈:

if (direction.isEastwards() || direction.isNorthwards()) { 
     Vector3 projectedPosition = getLocation().getCamera().project(
       new Vector3(16 * getX() + 1, Gdx.graphics.getHeight() 
         - (16 * getY()) - 15, 0)); 
     batch.draw(current.getKeyFrame(
       animTime += Gdx.graphics.getDeltaTime(), true), 
       projectedPosition.x, projectedPosition.y); 
    } else { 
     Vector3 projectedPosition = getLocation().getCamera().project(
       new Vector3(16 * getX() + 13, Gdx.graphics.getHeight() 
         - (16 * getY()) - 15, 0)); 
     batch.draw(current.getKeyFrame(
       animTime += Gdx.graphics.getDeltaTime(), true), 
       projectedPosition.x, projectedPosition.y); 

    } 

當我啓動遊戲在Eclipse中,精靈開始出現在正確的位置。但是,如果我調整屏幕大小,精靈將停止在正確的位置,並最終從地圖上消失。

在我開始使用投影之前發生了同樣的事情,我認爲問題與投影有關。由於這是一個我沒有探討過的領域,我決定在一個小時後能夠解決這個問題尋求幫助。

動畫根據角色面對的方向翻轉,這就是爲什麼if/else子句在那裏。

附錄:使用new Stage(new ExtendViewport(800,600),batch);創建舞臺,resize方法更新相機,並將批次設置爲投影矩陣。

下面是更多相關代碼:

相機和地圖初始化:

camera=new OrthographicCamera(); 
camera.setToOrtho(false,Gdx.graphics.getWidth(),Gdx.graphics.getHeight()); 
mapRenderer=new OrthogonalTiledMapRenderer(map,batch); 

Render方法:

camera.update(); 
batch.setProjectionMatrix(camera.combined); 
mapRenderer.setView(camera); 
mapRenderer.render(); 
stage.act(); 
stage.draw(); 

調整大小方法:

camera.viewportWidth=width; 
camera.viewportHeight=height; 
camera.update(); 

演員抽獎方法:

@Override 
public void draw(Batch batch, float parentAlpha) { 
    Color color = getColor(); 
    batch.setColor(color.r, color.g, color.b, color.a * parentAlpha); 
    if (direction.isEastwards() || direction.isNorthwards()) { 
     Vector3 projectedPosition = getLocation().getCamera().project(
       new Vector3(16 * getX() + 1, Gdx.graphics.getHeight() 
         - (16 * getY()) - 15, 0)); 
     batch.draw(current.getKeyFrame(
       animTime += Gdx.graphics.getDeltaTime(), true), 
       projectedPosition.x, projectedPosition.y); 
    } else { 
     Vector3 projectedPosition = getLocation().getCamera().project(
       new Vector3(16 * getX() + 13, Gdx.graphics.getHeight() 
         - (16 * getY()) - 15, 0)); 
     batch.draw(current.getKeyFrame(
       animTime += Gdx.graphics.getDeltaTime(), true), 
       projectedPosition.x, projectedPosition.y); 

    } 
    // Walk animation displays for 0.2 seconds 
    if (current == walk && animTime >= 0.2) { 
     current = idle; 
     animTime = 0; 
    } 
} 

回答

1

我覺得它造成的問題是因爲你不把精靈和相機投影結合起來。設置你的spriteBatch這樣的:

spriteBatch.setProjectionMatrix(camera.combined); 

而且在調整大小的方法,不要錯過更新視:

public void resize(int width, int height) { 
    camera.viewportWidth = width; 
    camera.viewportHeight = height; 
    camera.update(); 
} 

有了這一點,你可以調整或zoomin縮小(ZoomOut),它縮減規模到相機投影spriteBatch。

+0

我已經明確添加了'batch.setProjectionMatrix(camera.combined);'。我會檢查我是否忘記了更新線。 – pixelherodev

+0

更新了我的問題;增加了更多的相關代碼。 – pixelherodev

+0

您是否也在調整大小中應用視口的更新?像這樣:stage.getViewport()。update(Gdx.graphics.getWidth(),Gdx.graphics.getHeight,true);或任何你想要的屏幕尺寸? – Wouf