繁体   English   中英

如何在libgdx中将精灵表/纹理区域从1个角点设置为触摸点?

[英]How to animate a sprite sheet/texture regions from 1 corner to touch point in libgdx?

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

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

编辑:我的班级实现屏幕这是我试过的

在类默认构造函数中

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

调用触摸方法后用于动画

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

您只需从动画中获取合适的关键帧,然后批量绘制。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM