【问题标题】:Setting the Cursor Position设置光标位置
【发布时间】:2016-02-26 17:08:18
【问题描述】:

所以我有一个正在开发的游戏,一旦你完成游戏,我希望用户能够点击“再玩”按钮并能够在开始时重置。 为此,我在 Texture 上创建了一个 Rectangle 并使用 contains() 方法。

if(cloud.contains((float)Gdx.input.getX(),(float)(Gdx.graphics.getHeight()-Gdx.input.getY()))){
    reset();
}

重置方法:

public void reset(){
    speed=0;
    paraY = Gdx.graphics.getHeight() - para[parachuteState].getHeight();
    gameState=0;
    backY = 0-Gdx.graphics.getHeight();
    bach=0;
    Gdx.input.setCursorPosition(Gdx.graphics.getWidth()/2,Gdx.graphics.getHeight()/2);
}

所以发生的事情是程序识别出被按下的按钮并重置,但当游戏再次结束时,它会自动重置而不显示结束屏幕。我认为光标没有移动到中心,而是保留在按钮的顶部。我是错误地使用了 setCursorPosition() 方法还是有其他方法可以做到这一点?

【问题讨论】:

  • 但是你为什么不想用 Button 而不是 Texture 和 Rectangle 呢?
  • 阅读触摸监听器。看起来您一直在轮询最后一个已知的手指位置,而不是对屏幕上的新点击做出反应。 setCursorPosition 影响箭头在 PC 上的位置。它在 Android 上没有任何作用,因为没有光标。
  • 按钮会更简单吗?我有一张我在 Adob​​e Illustrator 中绘制的图像,我希望它可以点击@Enigo
  • 也感谢您提供有关触摸监听器的信息@Tenfour04
  • @ThomasS。看我的回答。如果您有任何问题,请随时询问:)

标签: java android libgdx


【解决方案1】:

这个按钮是正确的。它可能看起来更复杂,但它是推荐的方式来做你想做的事。 所以,要创建一个按钮,你应该这样做:

Skin skin = new Skin();
skin.addRegions(uiTextureAtlas);
TextButton.TextButtonStyle buttonStyle = new TextButton.TextButtonStyle();
buttonStyle.up = skin.getDrawable("textureName");
buttonStyle.font = font;
Button button = new Button(buttonStyle);
button.addListener(new InputListener() {
    @Override
    public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
        reset();
        return true;
    }
});
button.setSize(100, 100);
button.setPosition(300, 400);

然后在你的 Screen 类中创建

Stage stage = new Stage();
stage.addActor(button);
Gdx.input.setInputProcessor(stage);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-14
    • 1970-01-01
    相关资源
    最近更新 更多