How to change Sprite texture to Animation

爷,独闯天下 提交于 2019-12-02 22:12:03

问题


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

回答1:


To change from a texture to an animation

Create a subclas of Sprite called MySprite or something, and override the draw(batch) method.

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()

Why you have to specify 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.



来源:https://stackoverflow.com/questions/30774715/how-to-change-sprite-texture-to-animation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!