2016-11-26 51 views
0

我正在開發一個libGDX遊戲,我想添加自定義按鈕。當按下按鈕時,我想添加一個像scaleBy這樣的動作,因爲它在Android中看起來更好。我創建了一個名爲Trigger的類,它是一個空的演員,具有一個偵聽器,當觸發器被按下,輸入,釋放,退出時向actor添加動作...所以我創建了一個包含兩個actor的組:TextButton(Touchable.disabled)和觸發器。我不能在Dialog的按鈕方法中添加組。改爲使用操作使用libGDX中的按鈕繪製?

我認爲一個更好的解決方案是創建一個擴展TextButton或Button的類,但我不知道該怎麼做。

如果你想要的代碼,只是告訴。

感謝。

編輯:我試圖使類:

private Runnable runnable; 
private UIScreen screen; 

public static TextButtonStyle transformStyle(Skin skin) { 
    TextButtonStyle s = skin.get(TextButtonStyle.class); 
    TextButtonStyle style = new TextButtonStyle(s.up, null, null, s.font); 
    return style; 
} 

public static TextButtonStyle transformStyle(TextButtonStyle style) { 
    TextButtonStyle s = new TextButtonStyle(style.up, null, null, style.font); 
    return s; 
} 

public DTextButton(String text, Skin skin, UIScreen screen, Runnable runnable) { 
    super(text, transformStyle(skin)); 
    this.runnable = runnable; 
    this.screen = screen; 
    addListener(new ButtonListener()); 
    setOrigin(Align.center); 
    setTransform(true); 
} 

@Override 
public void draw(Batch batch, float parentAlpha) { 
    super.draw(batch, parentAlpha); 
} 

public class ButtonListener extends ClickListener { 

    private boolean isPressed = false; 
    private int lastButton = 0; 

    public void press() { 
     screen.setButtonPressed(true); 
     UIFactory.applyPressedAction(DTextButton.this); 
     isPressed = true; 
    } 

    public void release(){ 
     UIFactory.applyReleasedAction(DTextButton.this); 
     isPressed = false; 
    } 

    @Override 
    public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) { 
     if(button == Buttons.LEFT) 
      press(); 
     return true; 
    } 

    @Override 
    public void enter(InputEvent event, float x, float y, int pointer, Actor fromActor) { 
     lastButton = (Gdx.input.isButtonPressed(Buttons.LEFT)) ? Buttons.LEFT : -1; 
     if(pointer == 0 && !isPressed && screen.wasButtonPressed()) 
      press(); 
    } 

    @Override 
    public void exit(InputEvent event, float x, float y, int pointer, Actor toActor) { 
     if(toActor == DTextButton.this && lastButton == Buttons.LEFT){ 
      Gdx.app.postRunnable(runnable); 
     } 
     if(pointer == 0 && isPressed) 
      release(); 
    } 

    @Override 
    public void touchUp(InputEvent event, float x, float y, int pointer, int button) { 
     lastButton = button; 
     if(pointer == 0 && isPressed && button == Buttons.LEFT) 
      release(); 
     screen.setButtonPressed(false); 
    } 

} 

UIScreen:

public interface UIScreen { 

/** 
* Return if any UI is pressed. 
* @return return buttonPressed 
*/ 
public boolean wasButtonPressed(); 

/** 
* Set if any UI is pressed. 
* @param pressed if pressed or not. 
*/ 
public void setButtonPressed(boolean pressed); 

} 

UIFactory:

public static void applyPressedAction(Actor actor) { 
    actor.addAction(Actions.scaleBy(-0.2f, -0.2f, 0.2f, Interpolation.pow2Out)); 
} 

public static void applyReleasedAction(Actor actor) { 
    actor.addAction(Actions.scaleBy(0.2f, 0.2f, 0.3f, Interpolation.swingOut)); 
} 

所以我只用style.up和我說同樣的觸發器的偵聽器。聽衆不能很好地工作。任何建議?

+0

順便說一下,如果您在不調用super方法的情況下重寫它的方法,ClickListener將無法正常運行。 – Tenfour04

回答

0

我覺得你太過複雜了。您只能將一個輸入偵聽器添加到該按鈕,並確保它被設置爲轉換。你可能想要它的起源爲中心。不需要子類或包裝組或類似的東西。

此外,要通過規模,不,以避免錯誤建設爲你改變狀態多次。

這裏是一個輔助方法:

public static void makeButtonScale (final Button button, final float hoverScale, final float pressedScale, final float duration){ 
    button.setTransform(true); 
    button.addListener(new ClickListener(){ 
     void scaleTo (float targetScale){ 
      button.setOrigin(Align.center); 
      button.clearActions(); 
      button.addAction(Actions.scaleTo(targetScale, targetScale, duration, Interpolation.fade)); 
     } 

     public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) { 
      super.touchDown(event, x, y, pointer, button); 
      scaleTo(pressedScale); 
      return true; 
     } 

     public void touchUp (InputEvent event, float x, float y, int pointer, int button) { 
      super.touchUp(event, x, y, pointer, button); 
      scaleTo(isOver() ? hoverScale : 1f); 
     } 

     public void enter (InputEvent event, float x, float y, int pointer, Actor fromActor) { 
      super.enter(event, x, y, pointer, fromActor); 
      scaleTo(isPressed() ? pressedScale : hoverScale); 
     } 

     public void exit (InputEvent event, float x, float y, int pointer, Actor toActor) { 
      super.exit(event, x, y, pointer, toActor); 
      scaleTo(1f); 
     } 
    }); 

用法示例:

makeButtonScale(myDialogButton, 1.2f, 1.5f, 0.2f); 

注意,除非你正在使用你的紋理圖集線性MAG過濾器這不會很好看。