2013-09-25 99 views
0

此代碼在左右,上下移動時使圖像邊框閃爍(閃爍)。即使我使用Screen class render()方法增量值,爲什麼圖像邊框閃爍不動?移動時Sprite閃爍libgdx

package com.me.mygdxgame; 
    import com.badlogic.gdx.Gdx; 
    import com.badlogic.gdx.Input.Keys; 
    import com.badlogic.gdx.InputProcessor; 
    import com.badlogic.gdx.graphics.GL10; 
    import com.badlogic.gdx.graphics.OrthographicCamera; 
    import com.badlogic.gdx.graphics.Texture; 
    import com.badlogic.gdx.graphics.Texture.TextureFilter; 
    import com.badlogic.gdx.graphics.g2d.Sprite; 
    import com.badlogic.gdx.graphics.g2d.SpriteBatch; 
    import com.badlogic.gdx.math.Vector3; 
    public class MoveSpriteExample extends GdxTest implements InputProcessor { 
     Texture texture; 
     SpriteBatch batch; 
     OrthographicCamera camera; 
     Vector3 spritePosition = new Vector3(); 
     Sprite sprite; 
     public void resize (int width, int height) { 
     } 

     public void create() { 

      float w = Gdx.graphics.getWidth(); 
      float h = Gdx.graphics.getHeight(); 
      batch = new SpriteBatch(); 
      camera= new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); 
      camera.setToOrtho(false, w, h); 
      texture = new Texture(Gdx.files.internal("data/grasswall.png")); 
      texture.setFilter(TextureFilter.Linear, TextureFilter.Linear); 
      sprite = new Sprite(texture); 

      sprite.setSize(32, 32); 
      spritePosition.y=100; 

      sprite.setPosition(spritePosition.x,spritePosition.x); 
      lastUpdateTime = System.nanoTime(); 
     } 

    public void Update(float Delta) 
    { 
     if (Gdx.input.isKeyPressed(Keys.D)==true) 
     { 
      spritePosition.x += 150*(Delta/1000000000.0); 

     }else if (Gdx.input.isKeyPressed(Keys.A)==true) 
     { 
      spritePosition.x -= 150*(Delta/1000000000.0); 
     } 
     else if (Gdx.input.isKeyPressed(Keys.Z)==true) 
     { 
      spritePosition.y -= 150*(Delta/1000000000.0); 
     } 
     else if (Gdx.input.isKeyPressed(Keys.W)==true) 
     { 
      spritePosition.y += 150*(Delta/1000000000.0); 
     } 
    } 
    float lastUpdateTime; 
    float currentTime; 
     public void render() { 
      currentTime = System.nanoTime(); 
      Gdx.gl.glClearColor(1, 1, 1, 1); 
      Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); 

      camera.update(); 

      Update(currentTime - lastUpdateTime); 

      sprite.setPosition(spritePosition.x,spritePosition.y); 

      batch.setProjectionMatrix(camera.combined); 

      batch.begin(); 

      sprite.draw(batch); 

      batch.end(); 

      lastUpdateTime = currentTime; 
     } 
    } 

請提供代碼示例

+0

可能重複(http://stackoverflow.com/questions/18959312/flickering-object-image-movement-in-libgdx-desktop-app) – BennX

+0

是否有更多的渲染執行路徑上的代碼比你在這裏展示的要多少?我猜在'GdxTest'中有一個'render'來調用這個? –

回答

1

正如我說在你的帖子的最後,你應該看看教程! 首先你不需要自己計算三角洲時間。我已經在你的最後一篇文章中發佈瞭如何獲得它。

我給你一個非常短的例子,一個移動的精靈沒有閃爍。 起初,這裏是ApplicationListener。 (不是GDXtest)

public class MainClass implements ApplicationListener { 
    private Screen currentScreen = null; 

    @Override 
    public void create() { 
     Texture.setEnforcePotImages(false); 
     this.currentScreen = new TestScreen(); 
    } 

    @Override 
    public void dispose() { 
     this.currentScreen.dispose(); 

    } 

    @Override 
    public void render() { 
     this.currentScreen.render(Gdx.graphics.getDeltaTime()); 
    } 

    @Override 
    public void resize(int width, int height) { 
     this.currentScreen.resize(width, height); 
    } 

    @Override 
    public void pause() { 
     this.currentScreen.pause(); 
    } 

    @Override 
    public void resume() { 
     this.currentScreen.resume(); 
     ; 
    } 
} 

這是preaty簡單,因爲你可以看到它並調用屏幕自動用增量時間呈現! this.currentScreen.render(Gdx.graphics.getDeltaTime())

接下來你需要的是一個簡單的屏幕。所以一些實現Screeninterface的東西。我真的建議你在場景中使用Scene2D設置。但這裏沒有一個例子。

public class TestScreen implements Screen { 
    private Sprite mySprite; 
    private SpriteBatch batch = new SpriteBatch(); 

    public TestScreen() { 
     this.mySprite = new Sprite(new Texture(
       Gdx.files.internal("data/appicon.png"))); 
     this.mySprite.setPosition(50f, 50f); 
    } 

    @Override 
    public void render(float delta) { 
     Gdx.gl.glClearColor(1, 1, 1, 1); 
     Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); 
     this.update(delta); 
     this.batch.begin(); 
     this.mySprite.draw(batch); 
     this.batch.end(); 
    } 

    public void update(float delta) { 
     if (Gdx.input.isKeyPressed(Keys.D) == true) { 
      mySprite.setX(mySprite.getX() + 150 * delta); 
     } else if (Gdx.input.isKeyPressed(Keys.A) == true) { 
      mySprite.setX(mySprite.getX() - 150 * delta); 
     } else if (Gdx.input.isKeyPressed(Keys.Z) == true) { 
      mySprite.setY(mySprite.getY() - 150 * delta); 
     } else if (Gdx.input.isKeyPressed(Keys.W) == true) { 
      mySprite.setY(mySprite.getY() + 150 * delta); 
     } 
    } 

    @Override 
    public void resize(int width, int height) { 
    } 

    @Override 
    public void show() { 
    } 

    @Override 
    public void hide() { 
    } 

    @Override 
    public void pause() { 
    } 

    @Override 
    public void resume() { 
    } 

    @Override 
    public void dispose() { 
    } 
} 

請注意教程。 (Here is again the beginning of it)

[在libgdx桌面應用閃爍目標圖像移動]的
+0

主席先生,我仍然在我的圖像邊界閃爍 –

+0

主席先生,我仍然在移動時在我的圖像邊界閃爍。我已經測試德爾塔值從屏幕渲染()方法得到。簡單地說,我需要知道這是一個三角洲問題(manualy/libgdx增量值)或者如果我有任何版本問題 –

+0

不要使用過濾器 – BennX