[英]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.