java - 如何在 libgdx 中为从 1 个角到触摸点的 Sprite 表/纹理区域设置动画?

标签 java android opengl-es libgdx android-animation

我正在使用 Libgdx 游戏库开发游戏。 我在尝试使用从一个角点到接触点制作动画时遇到了这个问题 libgdx。 使用本教程了解 Libgdx 动画基础知识的基础知识 Link .

我无法找到 libgdx 中的动画是如何工作的,帧的正常移动发生但如何在触摸屏幕时启动单次正常动画。 在此先感谢您的帮助。

编辑: 我的类(class)实现屏幕 这是我试过的

在类默认构造函数中

animation=new Animation(1/15f, atlas1.getRegions());

在渲染方法中:- 检查触摸

public void touched()
{
    if(Gdx.input.isTouched())
    {
        touched = true;
        x = Gdx.input.getX();
        y = Gdx.input.getY();
        velocityX = (x - animationX) / 100;
        velocityY = (y - animationY) / 100;
    }
}

调用touch方法后做动画

public void anim()
{
    if (touched) 
    {
         elapsedTime += Gdx.graphics.getDeltaTime();
         //animationX += velocityX;
        // animationY += velocityY;
         batch.draw(animation.getKeyFrame(elapsedTime, false), x, y);
         animfinished=animation.isAnimationFinished(elapsedTime);
     }
    if(touched)
    {
          batch.draw(bullettouch, x, y, bullettouch.getRegionWidth(), bullettouch.getRegionHeight());
    }
}

最佳答案

您可以使用 InputListener用于触摸事件并使用它来控制您的动画。

下面是简单的方法。当然,更合适的做法是扩展 Animation 类并将所有动画逻辑移到那里。

public class AnimationTest implements ApplicationListener, InputListener {

    boolean touched = false;
    Animation animation;
    float elapsedTime = 0;
    float touchedX, touchedY;
    float animationX, animationY;
    float velocityX, velocityY;

    //  ... do not forget to register InputListener here ...

    @Override
    public boolean touchDown(InputEvent event, float x, float y, int pointer,int button) {
        touched = true;
        touchedX = x;
        touchedY = y;
        velocityX = (touchedX - animationX) / 100;
        velocityY = (touchedY - animationY) / 100;
    }

    // ... 
    
    @Override
    public void render() {
        Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

        batch.begin();

        if (touched) {
            elapsedTime += Gdx.graphics.getDeltaTime();
            animationX += velocityX;
            animationY += velocityY;
            batch.draw(animation.getKeyFrame(elapsedTime, true), animationX, animationY);
        }
        
        batch.end();
    }

}

这是动画发生的地方:

batch.draw(animation.getKeyFrame(elapsedTime, true), animationX , animationY);

您只需从动画中获取适当的关键帧并批量绘制即可。

关于java - 如何在 libgdx 中为从 1 个角到触摸点的 Sprite 表/纹理区域设置动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24775329/

相关文章:

android - 未找到心率传感器 (LG Watch Urbane)

iphone - Opengl 中的重复状态更改

java - 无法使 Selenium Firefox 驱动程序与 Java 一起工作

java - 在 actionListener 中将 JPanel 添加到 contentPane

java - Android 中的单例与应用程序上下文?

Android opengl 纹理损坏

Android queueEvent(new Runnable()) 如果我有多个线程托管渲染器怎么办?

java - 在 Webview 上启用 GeoLocation

android - 如何解决Android中的java.lang.OutOfMemoryError故障

java - W3C validator 返回 HTTP 500,尽管它可以在浏览器中运行