[英]LibGDX: Implement touch screen, without touching the screen
I'm using LibGDX. 我正在使用LibGDX。
Let's say that I wan't to jump - but not really touching the screen, I don't want to use a keyboard forsay, And implement the touch, like i was touching the screen. 假设我不想跳-但不是真正地触摸屏幕,我不想使用键盘作为键盘,而是像触摸屏幕一样实现触摸。 Can i do that? 我可以那样做吗?
Can one let's say "test" the button without pressing it (from the touch screen of application / android / ios / etc..)? 可以说不按下按钮(从应用程序/ android / ios /等的触摸屏)“测试”按钮吗?
Example of a button from InputListener: InputListener中的按钮示例:
final Image imageJumpButton = new Image(new Texture("jbjump.png"));
imageJumpButton.addListener(new InputListener() {
@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
JOYSTICK_UP= true;
return true;
}
@Override
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
JOYSTICK_UP = false;
}
});
I thank you. 我谢谢你。
Probably. 大概。 I'm not sure if this working. 我不确定这是否有效。 Can't test now. 现在无法测试。 You can use Actor fire(Event event)
method. 您可以使用Actor fire(Event event)
方法。
imageJumpButton.fire(InputEvent.Type.touchDown);
or 要么
imageJumpButton.fire(InputEvent.Type.touchUp);
I would suggest to use Button
class for a button instead of Image
. 我建议对按钮而不是Image
使用Button
类。 There is a convenient way to do what you ask with Button
: 使用Button
可以方便地完成您的要求:
final Button imageJumpButton = new Button(new TextureRegionDrawable(new TextureRegion(new Texture("jbjump.png"))));
//use ChangeListener instead of InputListener
imageJumpButton.addListener(new ChangeListener() {
@Override
public void changed(ChangeEvent event, Actor actor) {
Gdx.app.log("", "Button is pressed");
}
});
Then you could programmatically press the button: 然后您可以以编程方式按下按钮:
//this will trigger changed() method of the ChangeListener
imageJumpButton.toggle();
Update 更新资料
If you want to do something, while the button is pressed, call this in your render()
method: 如果您想做某事,请在按下按钮的同时在render()
方法中调用此方法:
if (button.isPressed())
//do something each frame, while button is pressed
For example, if you want a player to run, while the button is pressed, it would be something like this: 例如,如果您想让玩家在按下按钮的同时跑步,那就是这样的:
public void update(float deltaTime) {
if (button.isPressed())
player.x += deltaTime * velocityX;
}
And by the way, you don't need to add ChangeListener to the button in this case. 顺便说一下,在这种情况下,您不需要将ChangeListener添加到按钮。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.