[英]Use actions instead Drawable with buttons in libGDX?
我正在libGDX中開發游戲,我想添加自定義按鈕。 當按下按鈕時,我想添加一個類似於scaleBy的動作,因為它看起來更像Android中的動作。 我制作了一個名為Trigger的類,它是一個空的actor,帶有一個偵聽器,可以在按下,輸入,釋放,退出Trigger時將動作添加到actor。因此,我創建了一個具有兩個actor的Group:TextButton(Touchable.disabled)和觸發器。
它確實有效,但是我有一個問題:我無法在Dialog的button方法中添加組。
我認為更好的解決方案是創建一個擴展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,並添加了相同的Trigger偵聽器。 監聽器效果不佳。 有什么建議嗎?
我認為您過於復雜了。 您只需將輸入偵聽器添加到按鈕,並確保將其設置為可轉換即可。 您可能希望其起源居中。 不需要子類或包裝器組或類似的東西。
此外,要縮放,也不由 ,以避免錯誤建設為你改變狀態多次。
這是一個輔助方法:
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濾鏡,否則效果將不佳。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.