设置光标位置

时间:2016-02-26 17:08:19

标签: java android libgdx

所以我有一个我正在玩的游戏,一旦你完成游戏我希望能够让用户点击“再次播放”按钮并能够在开始时重置。 为此,我在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()方法,还是有其他方法可以做到这一点?

1 个答案:

答案 0 :(得分:0)

按钮就是正确的。它可能看起来更复杂,但建议您按照自己的意愿行事。 因此,要创建一个按钮,您应该执行以下操作:

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);
相关问题