无法使用Tween在Libgdx中为sprite设置动画

时间:2017-04-18 06:48:44

标签: android opengl-es libgdx tween universal-tween-engine

我正在开发像Candy Crush这样的游戏,我一直面临着在用户升级时弹出的问题。我最近发布了一个问题,以显示字体弹出并解决它: Unable to Draw Sprite after Camera.Update in LibGDX

但现在我想使用Tween引擎弹出该弹出窗口并且由于某种原因它无法正常工作。我在游戏的其他部分使用过Tween引擎让硬币反弹并逐渐消失,这对于下面的代码工作正常。

褪色动画:

plusTen = new Sprite(ten);
            plusTen.setSize(1, 1);
            plusTen.setPosition(object.getX(), object.getY());


            Tween.set(plusTen, SpriteAccessor.ALPHA).target(1).start(game.tweenManager);
            to(plusTen, SpriteAccessor.ALPHA, 0.7f).target(0).delay(0.7f).start(game.tweenManager);

弹跳动画:

  sprite.setSize(1, 1);
            sprite.setPosition(columnIndex, this.MAX_ROWS + 5);

            sprite.setPosition(columnIndex, this.MAX_ROWS + 5);
            Tween.to(sprite, Accessor.POSITION_Y, 0.8f)
                    .target(sprite.getY() - moveBy - 5)
                    .ease(Bounce.OUT)
                    .start(game.tweenManager);

以上两点都有效。但是对于我在上面链接中提到的弹出窗口,我想创建像弹性一样的动画(更大 - 更小 - 更大 - 更小)并且它不起作用。

我使用以下代码与链接中显示的代码合并: Unable to Draw Sprite after Camera.Update in LibGDX

private void initLevelUpAnimation() {


    Log.d("Runnable", "animation method called");

    levelUp = new Sprite(levelUpBG);
    levelUp.setSize(265, 180);
    levelUp.setPosition(gamePlayBG.getWidth() / 2 - levelUp.getWidth() / 2,
            gamePlayBG.getHeight() / 2 - levelUp.getHeight() / 2);


    Tween.to(levelUp, Accessor.POSITION_XY, 3.0f)
            .ease(Elastic.INOUT)
            .target(50,50)
            .start(game.tweenManager);


    stopGameObjects = true;


}

这是我的render()方法代码:

public void render(float delta) {
    // TODO Auto-generated method stub

    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);


    game.batch.begin();

    game.batch.setProjectionMatrix(camera2.combined);

    game.batch.draw(gamePlayBG, 0, 0);


    if (gameOver != true) {
        //Log.d("render------------", "render: "+ delta);


    camera.update();

    game.batch.setProjectionMatrix(camera.combined);


    if (lineHud.size > 1) {
        for (Sprite sprite : this.lineHud) {

            sprite.draw(game.batch);
        }
    }


    for (int i = 0; i < gameObjects.size; i++) {
        gameObjects.get(i).draw(game.batch);
    }


    for (Sprite sprite : this.scoreHud) {

        sprite.draw(game.batch);


    }


    if (stopGameObjects) {

        game.batch.setProjectionMatrix(camera2.combined);

        levelUp.draw(game.batch);

        game.levelUpFont.drawMultiLine(game.batch, "LEVEL " + (baseLevel + 1),
                (gamePlayBG.getWidth() / 2) - 55,
                (gamePlayBG.getHeight() / 2) + 35);

        game.levelUpFont.drawMultiLine(game.batch, "KEEP GOING",
                (gamePlayBG.getWidth() / 2) - 90,
                gamePlayBG.getHeight() / 2);


    }


    game.tweenManager.update(delta);

    game.batch.end();


    update(delta);


    game.batch.setProjectionMatrix(camera2.combined);


}

这是我的SpriteAccessor类:

public class SpriteAccessor implements TweenAccessor<Sprite> {
public static final int ALPHA = 0;
@Override
public int getValues(Sprite target, int tweenType, float[] returnValues) {
    switch (tweenType){
        case ALPHA:
            returnValues[0] = target.getColor().a;
            return 1;
        default:
            assert false;
            return  -1;
    }

}

@Override
public void setValues(Sprite target, int tweenType, float[] newValues) {
    switch (tweenType){
        case ALPHA:
            target.setColor(target.getColor().r, target.getColor().g, target.getColor().b, newValues[0]);
            break;
        default:
            assert false;
    }

}

}

请提出我的错误建议。

1 个答案:

答案 0 :(得分:1)

Accessor下方使用您想要补间的几乎所有类型的媒体资源,您使用Accessor.POSITION_XY补充了您的levelUp Sprite的xy位置,但您的要求是扩展和你的精灵。

您必须在精灵上调用setOrigin将原点设置为中间,否则默认为左下角。原点用于缩放和旋转。

public class SpriteAccessor implements TweenAccessor<Sprite> {

    public static final int POS_XY = 1;
    public static final int CPOS_XY = 2;
    public static final int SCALE_XY = 3;
    public static final int ROTATION = 4;
    public static final int OPACITY = 5;
    public static final int TINT = 6;

    @Override
    public int getValues(Sprite 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;

            case SCALE_XY:
                returnValues[0] = target.getScaleX();
                returnValues[1] = target.getScaleY();
                return 2;

            case ROTATION: returnValues[0] = target.getRotation(); return 1;
            case OPACITY: returnValues[0] = target.getColor().a; return 1;

            case TINT:
                returnValues[0] = target.getColor().r;
                returnValues[1] = target.getColor().g;
                returnValues[2] = target.getColor().b;
                return 3;

            default: assert false; return -1;
        }
    }

    @Override
    public void setValues(Sprite 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;
            case SCALE_XY: target.setScale(newValues[0], newValues[1]); break;
            case ROTATION: target.setRotation(newValues[0]); break;

            case OPACITY:
                Color c = target.getColor();
                c.set(c.r, c.g, c.b, newValues[0]);
                target.setColor(c);
                break;

            case TINT:
                c = target.getColor();
                c.set(newValues[0], newValues[1], newValues[2], c.a);
                target.setColor(c);
                break;

            default: assert false;
        }
    }
}