移动libgdx时精灵闪烁

此代码在向左,向右,向下或向上移动时使图像边框闪烁(闪烁)。 为什么即使我使用Screen类render()方法delta值,图像边框闪光也会移动。

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; } } 

请提供带样本的代码

正如我在你上一篇文章中所说,你应该看看教程! 首先,您不需要自己计算增量时间。 我已经在你的上一篇文章中提到了你如何得到它。

我给你一个超短的例子,一个没有闪烁的移动精灵。 这里首先是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(); ; } } 

它很简单,你可以看到它确实用delta时间自动调用屏幕渲染! 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() { } } 

Regardas并确实看一下教程。 (这又是它的开头)