我想使用Tween引擎對文本按鈕執行縮放動畫。 我用它們各自的訪問器對精靈和字體使用了相同的代碼。但只有TextButton,代碼不起作用。下面是代碼:無法對Libgdx中的TextButton執行縮放動畫
private void initLevelUpAnimation() {
Log.d("Runnable", "animation method called");
Skin skin = new Skin(); //empty constructor causes it not to load yet.
skin.add("level_new", levelUpBG);
levelupButtonStyle = new TextButton.TextButtonStyle();
levelupButtonStyle.font = game.levelUpFont;
levelupButtonStyle.up = skin.getDrawable("level_new");
levelupButtonStyle.down = skin.getDrawable("level_new");
levelupButtonStyle.over = skin.getDrawable("level_new");
levelupButtonStyle.disabled = skin.getDrawable("level_new");
levelupButton = new TextButton("LEVEL " + (baseLevel + 1) + "\n" + "KEEP GOING", levelupButtonStyle);
levelupButton.setSize(265, 180);
levelupButton.setPosition(gamePlayBG.getWidth()/2 - levelupButton.getWidth()/2,
gamePlayBG.getHeight()/2 - levelupButton.getHeight()/2);
Log.d("TWEEN", "Read to animate: ");
Tween.to(levelupButton, ButtonAccessor.SCALE_XY, 0.01f)
.ease(Elastic.INOUT)
.target(0.1f, 0.1f)
.setCallback(new TweenCallback() {
@Override
public void onEvent(int type, BaseTween<?> source) {
stopGameObjects = true;
Log.d("TWEEN", "First completed : 0.3");
Tween.to(levelupButton, ButtonAccessor.SCALE_XY, 0.7f)
.ease(Elastic.INOUT)
.target(1.1f, 1.1f)
.setCallback(new TweenCallback() {
@Override
public void onEvent(int type, BaseTween<?> source) {
Log.d("TWEEN", "Second completed : 0.3");
Tween.to(levelupButton, ButtonAccessor.SCALE_XY, 0.7f)
.ease(Elastic.INOUT)
.target(0.8f, 0.8f)
.setCallback(new TweenCallback() {
@Override
public void onEvent(int type, BaseTween<?> source) {
Log.d("TWEEN", "Third completed : 0.3");
Tween.to(levelupButton, ButtonAccessor.SCALE_XY, 1.0f)
.ease(Elastic.OUT)
.target(1.3f, 1.3f)
.setCallback(new TweenCallback() {
@Override
public void onEvent(int type, BaseTween<?> source) {
Log.d("TWEEN", "Fourth completed : 0.7");
Tween.to(levelupButton, ButtonAccessor.ROTATION, 1.0f)
.ease(Elastic.OUT)
.target(1.0f, 1.0f)
.setCallback(new TweenCallback() {
@Override
public void onEvent(int type, BaseTween<?> source) {
Log.d("TWEEN", "Fifth completed : 1.0");
stopGameObjects = false;
updateScoreTime();
}
})
.start(game.tweenManager);
}
})
.start(game.tweenManager);
}
})
.start(game.tweenManager);
}
})
.start(game.tweenManager);
}
})
.start(game.tweenManager);
}
以下是我的ButtonAccessor:
public class ButtonAccessor implements TweenAccessor<TextButton> {
public static final int ALPHA = 0;
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(TextButton target, int tweenType, float[] returnValues) {
switch (tweenType) {
case ALPHA:
returnValues[0] = target.getColor().a;
return 1;
case SCALE_XY:
returnValues[0] = target.getScaleX();
returnValues[1] = target.getScaleY();
return 2;
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(TextButton target, int tweenType, float[] newValues) {
switch (tweenType) {
case ALPHA:
target.setColor(target.getColor().r, target.getColor().g, target.getColor().b, newValues[0]);
break;
case SCALE_XY:
target.setScale(newValues[0], newValues[1]);
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;
}
}
}
在渲染方法:
game.batch.setProjectionMatrix(camera2.combined);
levelupButton.draw(game.batch, 1);
按鈕被繪製在屏幕上,但沒有設置動畫。任何人都可以告訴我我在這裏做錯了什麼?
我試着從https://github.com/libgdx/libgdx/blob/master/gdx/src/com/badlogic/gdx/scenes/scene2d/ui/Container.java下載這個類 但是很多東西都丟失了我爲Libgdx使用的版本。 –
你爲什麼不更新你的項目到最新版本的libgdx? – Aryan
如果您依賴gradle,那麼您可以輕鬆地將您的項目升級到最新版本。 – Aryan