我有一个每秒生成的 Sprite ,我不想做的是将 Sprite 纹理更改为动画,当它被触摸时,它将恢复为正常纹理。
public void draw(SpriteBatch batch){
enemyIterator=enemies.iterator(); //arraylist iterator
boolean touched=Gdx.input.justTouched();
float touchX=Gdx.input.getX();
//rendering and making the current sprite move
while(enemyIterator.hasNext()){
Sprite sprite=enemyIterator.next();
sprite.draw(batch);
sprite.translateY(deltaTime*movement);
//detecting if the screen is touched and if the inputX is inside of the sprite.
if(touched==true && touchX > sprite.getX() && touchX < sprite.getX()+sprite.getWidth()){
enemyIterator.remove(); //removing the sprite when touched.
Pools.free(sprite); //freeing the Pools
}
}
最佳答案
从纹理更改为动画
创建一个名为 MySprite 或其他名称的 Sprite 子类,并覆盖 draw(batch)
方法。
在重写的draw方法中,如果要绘制纹理,只需调用super.draw(batch)
即可,另外使用您的动画绘制代码。您可以使用 Gdx.graphics.getDeltaTime()
获取增量时间
为什么必须指定 timePassed
你的程序将以与动画不同的帧速率运行,因此通过告诉动画已经过去了多少时间,它可以根据自己的帧速率计算出应该在哪一帧上。
请注意,您的应用的帧速率可能因帧而异,具体取决于它需要完成的工作量。
关于java - 如何将 Sprite 纹理更改为动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30774715/