簡體   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