2017-04-20 72 views
0

我想使用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); 

按鈕被繪製在屏幕上,但沒有設置動畫。任何人都可以告訴我我在這裏做錯了什麼?

回答

0

默認情況下變換設定爲最scene.ui元素由於性能原因而錯誤,因此您需要使用相同的容器或表格。

TextButton button = new TextButton("Text Button", skin); 
Container wrapper = new Container(button); 
wrapper.setTransform(true); 
wrapper.setOrigin(wrapper.getPrefWidth()/2, wrapper.getPrefHeight()/2); 
wrapper.setRotation(45); 
wrapper.setScaleX(1.5f); 

https://github.com/libgdx/libgdx/wiki/Scene2d.ui#rotation-and-scale

可以在TweenAccessor用戶使用的容器,然後應用動畫。

public class ContainerAccessor implements TweenAccessor<Container> { 

public static final int SCALE_XY = 3; 

@Override 
public int getValues(Container target, int tweenType, float[] returnValues) { 
    switch (tweenType) { 

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

     default: 
      assert false; 
      return -1; 
    } 

} 

@Override 
public void setValues(Container target, int tweenType, float[] newValues) { 
    switch (tweenType) { 

     case SCALE_XY: 
      target.setScale(newValues[0], newValues[1]); 
      break; 

     default: 
      assert false; 
    } 

} 
} 
+0

我試着從https://github.com/libgdx/libgdx/blob/master/gdx/src/com/badlogic/gdx/scenes/scene2d/ui/Container.java下載這個類 但是很多東西都丟失了我爲Libgdx使用的版本。 –

+0

你爲什麼不更新你的項目到最新版本的libgdx? – Aryan

+0

如果您依賴gradle,那麼您可以輕鬆地將您的項目升級到最新版本。 – Aryan

0

地址:button.setTransform(true);你的init方法

此處瞭解詳情:Why Libgdx's Table does not accept scale action?(一TEXT按鈕伸出表)

但我會建議使用階段,考慮scene2D行動。這確實同爲通用吐溫引擎,但更容易使用的scene2D演員(像你已經在使用的按鈕)

此處瞭解詳情:https://github.com/libgdx/libgdx/wiki/Scene2d#actions

+0

我已經有了。仍然不起作用。 –

+0

太棒了,那你就是浪費我們倆的時間!請張貼所有相關的代碼.. 這可能是.draw()方法不使用該比例而不使用舞臺。 –

+0

更新了答案(未經測試)。如果這不起作用,則需要一個階段 –