简体   繁体   English

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

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

I was developing a game using the Libgdx game library. 我正在使用Libgdx游戏库开发游戏。 I came up with this problem while trying to animate from one corner point to touch point using libgdx. 我尝试使用libgdx从一个角点动画到触摸点时想出了这个问题。 Used this tutorial for understanding basics of Libgdx animation basics Link . 使用本教程了解Libgdx动画基础知识链接的基础知识。

I am not able to find how animations in libgdx works,Normal movement of frames takes place but how to start single time normal animation upon touch on screen. 我无法找到libgdx中的动画如何工作,帧的正常移动发生,但如何在屏幕上触摸时启动单次正常动画。 Thanks in advance for help. 在此先感谢您的帮助。

Edit: My class implements Screen This is what i tried 编辑:我的班级实现屏幕这是我试过的

In class default constructor 在类默认构造函数中

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

In Render method :- To check touch 在渲染方法中: - 检查触摸

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

After calling touch method For animation 调用触摸方法后用于动画

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

You can use InputListener for touch event and use this to control your animation. 您可以将InputListener用于触摸事件,并使用它来控制动画。

Below is simple approach. 以下是简单的方法。 Of course, more proper would be to extend Animation class and move there all animation logic. 当然,更合适的是扩展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();
    }

}

This is where animation occurs: 这是动画发生的地方:

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

You just gets appropiate keyframe from your animation and draws this on batch. 您只需从动画中获取合适的关键帧,然后批量绘制。

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