[英]Moving a sprite in LibGDX
我在Java和Slick2D方面有幾年的經驗,由於它是一個更好的庫,因此我試圖將游戲移植到libgdx上。 但是,我在屏幕上移動精靈時遇到一個簡單的問題。 這個API一定有一些我不理解的范例。 我將代碼精簡如下。 它可以識別輸入,並通過我的整個實體和網絡系統(往返於服務器)運行輸入,但sprite仍位於同一位置。 任何幫助都會很棒。
我的create()方法:
public void create() {
//Check to see if we are using external server or localhost
ClientNetworkManager.setLocalhostAsHost(Constant.useLocalhost, this);
//Begin application setup
float w = Gdx.graphics.getWidth();
float h = Gdx.graphics.getHeight();
camera = new OrthographicCamera(1280,900);
batch = new SpriteBatch();
//Load assets into memory
texture = new Texture(Gdx.files.internal("Sprites/Ash.png"));
//texture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
sprite = new Sprite(texture);
sprite.setSize(256,256);
sprite.setPosition(44, 666);
//Begin external network setup
setPlayerPointer(null);
this.connection = new ClientConnectionThread(this);
getClientConnectionThread().setCurrentPing(0);
}
My render() method:
public void render() {
//Render logic
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
if (connection.isActive() && !Constant.performEssentialTasksOnly) {
InputManager.handlePlayerInput(this);
batch.setProjectionMatrix(camera.combined);
camera.update();
batch.begin();
batch.draw(sprite,playerPointer.getCurrentX(),playerPointer.getCurrentY());
System.out.println(playerPointer.getCurrentX());
batch.end();
}
}
getCurrentX / Y方法的值均正確報告,因此這不是問題。 我覺得這一定很明顯。
你可以打電話
sprite.setX(xpos);
更改實際位置。
如果要使用tiledmap,請不要忘記推入正確的SpriteBatch
,它應該由renderer.getSpriteBatch()
方法返回。
您沒有使用Sprite位置來渲染它,實際上是像在使用常規TextureRegion一樣使用它。
batch.draw(sprite,playerPointer.getCurrentX(),playerPointer.getCurrentY());
System.out.println(playerPointer.getCurrentX());
您在哪里更改此playerPointer?
像這樣做:
sprite.setPosition(playerPointer.getCurrentX(),playerPointer.getCurrentY())
sprite.draw(batch);
但是同樣,問題是playerPointer沒有正確的值(它們沒有改變)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.