【问题标题】:LibGDX: Implement touch screen, without touching the screenLibGDX:实现触摸屏,无需触摸屏幕
【发布时间】:2017-12-06 12:49:47
【问题描述】:

我正在使用 LibGDX。

假设我不想跳跃 - 但不是真正触摸屏幕,我不想使用键盘,并且实现触摸,就像我在触摸屏幕一样。 我可以这样做吗?

我们可以说“测试”按钮而不按下它吗(从应用程序/android/ios/等的触摸屏)?

来自 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;
    }
});

谢谢你。

【问题讨论】:

    标签: java android libgdx touchscreen


    【解决方案1】:

    我建议使用Button 类作为按钮而不是Image。使用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");
            }
        });
    

    然后您可以通过编程方式按下按钮:

    //this will trigger changed() method of the ChangeListener
    imageJumpButton.toggle();
    

    更新

    如果您想在按下按钮时做某事,请在您的 render() 方法中调用它:

    if (button.isPressed())
        //do something each frame, while button is pressed
    

    例如,如果您希望播放器在按下按钮时运行,它会是这样的:

    public void update(float deltaTime) {
        if (button.isPressed())
            player.x += deltaTime * velocityX;
    }
    

    顺便说一句,在这种情况下,您不需要将 ChangeListener 添加到按钮。

    【讨论】:

    • 我需要以一种方式使用此按钮:按住时做某事,不按住时不做任何事情。例如运行按钮。谢谢。
    • 更新了答案
    【解决方案2】:

    大概吧。我不确定这是否有效。现在不能测试。您可以使用 Actor fire(Event event) 方法。

    imageJumpButton.fire(InputEvent.Type.touchDown);
    

    imageJumpButton.fire(InputEvent.Type.touchUp);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-05
      • 2016-08-13
      • 2015-11-10
      • 1970-01-01
      • 2011-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多