我正在開發一個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和我說同樣的觸發器的偵聽器。聽衆不能很好地工作。任何建議?
順便說一下,如果您在不調用super方法的情況下重寫它的方法,ClickListener將無法正常運行。 – Tenfour04