触摸时将对象数组元素补间到一个角-LibGdx

Tween object array elements to one corner on touch-LibGdx

在我的游戏中,我有一系列硬币出现在特定的 action.Once 我触摸硬币,硬币必须平稳地(像飞一样)移动到屏幕的一个角落,一个接一个。 我正在像这样创建和绘制硬币数组:

private Coin coins[] = new Coin[10];//coin array

for(int i=0;i<coins.length;i++) {
    coins[i]=objectFactory.createCoin();//creating object array of coins
}

抽金币

 for(int i=0;i<coins.length;i++) {
    coinTexture = coinAnimation.getKeyFrame(animationTime, true);
    batch.draw(coinTexture,coins[i].getX(), coins[i].getY());
}

用于检测硬币触摸:

if(Gdx.input.isTouched()) {
        Vector3 touchPos = new Vector3();
        touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
         game.camera.unproject(touchPos);
         for(int i=0;i<coins.length;i++){
             Rectangle textureBounds=new Rectangle(coins[i].getX(),coins[i].getY(),coins[i].getWidth(),coins[i].getHeight());    
             if(textureBounds.contains(touchPos.x,touchPos.y)) {
                 System.out.println("u touched the coin!!!!!!!!");
             }
        }
}

我现在想用 Universal TweenEngine 来补间它触摸到的角 screen.I 我对补间引擎的概念是全新的。 我找不到任何有关如何使用补间实现此效果的有用文档 engine.Any 帮助将不胜感激。

在你的项目中添加补间引擎,在核心模块中注入这些依赖

compile 'com.github.arcnor:universal-tween-engine:6.3.4'
compile 'com.github.arcnor:universal-tween-engine:6.3.4:sources'

这些工件在 https://jitpack.io 中可用,因此请在项目中添加此存储库。

repositories {
    maven { url "https://jitpack.io" }
}

创建CoinAccessor

public class CoinAccessor implements TweenAccessor<Coin> {

    public static final int POS_XY = 1;
    public static final int CPOS_XY = 2;

    @Override
    public int getValues(Coin target, int tweenType, float[] returnValues) {
        switch (tweenType) {
            case POS_XY:
                returnValues[0] = target.getX();
                returnValues[1] = target.getY();
                return 2;

            case CPOS_XY:
                returnValues[0] = target.getX() + target.getWidth()/2;
                returnValues[1] = target.getY() + target.getHeight()/2;
                return 2;

            default: assert false; return -1;
        }
    }

    @Override
    public void setValues(Coin target, int tweenType, float[] newValues) {
        switch (tweenType) {
            case POS_XY: target.setPosition(newValues[0], newValues[1]); 
            break;

            case CPOS_XY: target.setPosition(newValues[0] - target.getWidth()/2, newValues[1] - target.getHeight()/2); 
            break;

            default: assert false;
        }
    }
}

Coin 注册 CoinAccessor 并更新 TweenManager

适当触摸硬币时

Tween.to(coin[i], CoinAccessor.POS_XY, 0.8f).target(targetX,targetY).start(tweenManager);

编辑

注册有静态方法registerAccessor

Tween.registerAccessor(Coin.class, new CoinAccessor());

在游戏的 update/render 方法中调用 TweenManagerupdate() 方法来更新补间管理器。

TweenManager tweenManager = new TweenManager();
tweenManager.update(dt);   // this call should be in render/update method