Unable to animate sprite in Libgdx using Tween

故事扮演 提交于 2019-12-10 12:24:00

问题


I am working on a game like Candy Crush and I have been facing issues with making a pop up while user Levels Up. I recently posted a question to display pop up in font and got it solved : Unable to Draw Sprite after Camera.Update in LibGDX

But now I want to animate that pop up using Tween Engine and It is not working for some reason. I have used Tween Engine in other parts of the game to make coins bounce and fade away, which is working fine with below code.

Fading Animation :

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

Bounce Animation :

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

Both of the above are working. But for the pop up which I create mentioned in the above link, I want to create animation like elastic (going bigger-smaller-bigger-smaller) and It is not working.

I am using below code merged with the code shown in link : 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;


}

Here is my code for render() method :

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


}

This is my SpriteAccessor class :

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

}

}

Please give your suggestions for what I am doing wrong.


回答1:


Use below Accessor that having almost all type of property that you want to tween, You're using Accessor.POSITION_XY for tweening your xy postion of levelUp Sprite but your requirement is to scale up and down your Sprite.

You must call setOrigin on your sprite to set the origin to the middle otherwise it defaults to the bottom left corner. The origin is used for scaling and rotation.

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


来源:https://stackoverflow.com/questions/43465424/unable-to-animate-sprite-in-libgdx-using-tween

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!