簡體   English   中英

設置光標位置

[英]Setting the Cursor Position

因此,我有一個正在開發的游戲,一旦完成游戲,我希望用戶可以點擊“再次播放”按鈕,並能夠在開始時進行重置。 為此,我在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()方法,或者還有其他方法可以做到這一點?

該按鈕將是正確的。 它可能看起來更復雜,但是建議您按照自己的方式去做。 因此,要創建按鈕,您應該執行以下操作:

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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM