如何将 Sprite 纹理更改为动画 [英] How to change Sprite texture to Animation

查看:39
本文介绍了如何将 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 纹理更改为动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆