How to change Sprite texture to Animation(如何将 Sprite 纹理更改为动画)
问题描述
我有一个每秒生成的精灵,我不想将精灵纹理更改为动画,当它被触摸时它会恢复为正常纹理.
I have a Sprite that spawns every second, what I wan't to do is change the sprite texture to animation, and the when it's touched it will be back to a normal texture.
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)
方法.
Create a subclas of Sprite called MySprite or something, and override the draw(batch)
method.
在覆盖的draw方法中,如果要绘制纹理,只需调用super.draw(batch)
,其他的使用你的动画绘制代码.您可以使用 Gdx.graphics.getDeltaTime()
In the overriden draw method, if you want to draw a texture, simply call super.draw(batch)
, otehrwise use your animation draw code. You can get the delta time using Gdx.graphics.getDeltaTime()
为什么必须指定 timePassed
你的程序会以与动画不同的帧率运行,所以通过告诉动画已经过去了多少时间,它可以根据自己的帧率计算出它应该在哪一帧.
Your program will run at different frame rates to the animation, so by telling the animation how much time has passed, it can work out what frame it should be on according to it's own framerate.
请注意,您的应用的帧速率可能因帧而异,具体取决于它需要完成的工作量.
Note that the framerate of your app can vary from frame-to-frame depending on how much work it has to do.
这篇关于如何将 Sprite 纹理更改为动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何将 Sprite 纹理更改为动画
基础教程推荐
- 如何强制对超级方法进行多态调用? 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01