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
 - 验证是否调用了所有 getter 方法 2022-01-01
 - 如何在 Spring @Value 注解中正确指定默认值? 2022-01-01
 - 多个组件的复杂布局 2022-01-01
 - Java Swing计时器未清除 2022-01-01
 - Java 实例变量在两个语句中声明和初始化 2022-01-01
 - 如何在 JFrame 中覆盖 windowsClosing 事件 2022-01-01
 - 在 Java 中创建日期的正确方法是什么? 2022-01-01
 - 从 python 访问 JVM 2022-01-01
 - 不推荐使用 Api 注释的描述 2022-01-01
 
    	
    	
    	
    	
    	
    	
    	
    	
						
						
						
						
						
				
				
				
				