LibGDX按鈕在觸摸時沒有響應

[英]LibGDX Button not responding on touch


My first problem is that the textfield is not showing up and the second is that the button is not responding. My idea is when image 'vault1' is touched the textfield should show 5.

我的第一個問題是文本字段沒有顯示,第二個問題是按鈕沒有響應。我的想法是當圖像'vault1'被觸摸時,文本字段應該顯示5。

This is what I have come up with and I can't figure out what I am doing wrong.

這就是我想出來的,我無法弄清楚我做錯了什么。

(I have only cut out neseccary code)

(我只刪除了neseccary代碼)

public void render(float delta) {

    drawSprite("vault1", 600, 450);
}

public void show() {

    Gdx.input.setInputProcessor(stage);
    skin = new Skin(Gdx.files.internal("ui/menuSkin.json"), new TextureAtlas("ui/atlas.pack"));

    vaultInput = new TextField("", skin);
    vaultInput.setPosition(300, 250);
    vaultInput.setSize(300, 40);
    vaultInput.setText(text2);
    vault1Button = new Image(vault1);

    vault1Button.addListener(new ClickListener() {
        public boolean touchDown(InputEvent e, float x, float y, int point, int button) {
            System.out.println("133");
           vaultInput.setText(score);
            return false;
        }});
}

private void drawSprite(String name, float x, float y) {
    Sprite sprite = textureAtlas.createSprite(name);

    sprite.setPosition(x, y);

    sprite.draw(batch);
    vault1 = sprite;
}

2 个解决方案

#1


2  

It's hard to tell when you've left out code but you are adding the stage to the input processor:

當你遺漏代碼但是你要將階段添加到輸入處理器時很難分辨:

 Gdx.input.setInputProcessor(stage);

The stage and actors added to the stage is now the only things that responds to input. But you are never adding anything to the stage and neither drawing the stage, you are drawing your sprites seperately in the drawSprite method.

舞台和演員添加到舞台現在是唯一響應輸入的東西。但是你永遠不會在舞台上添加任何東西,也不會繪制舞台,你在drawSprite方法中單獨繪制你的精靈。

If you instead add your button to the stage all you need to do is call stage.draw():

如果你改為將你的按鈕添加到舞台上,你需要做的就是調用stage.draw():

public void render(float delta) {
    stage.draw();
    stage.act(delta);
}

public void show() {
   Gdx.input.setInputProcessor(stage);
       skin = new Skin(Gdx.files.internal("ui/menuSkin.json"), new TextureAtlas("ui/atlas.pack"));


    vaultInput = new TextField("", skin);
    vaultInput.setPosition(300, 250);
    vaultInput.setSize(300, 40);
    vaultInput.setText(text2);
    vault1Button = new Image(vault1);


    vault1Button = new Image(vault1);



    vault1Button.addListener(new ClickListener() {
    public boolean touchDown(InputEvent e, float x, float y, int point, int button) {
            System.out.println("133");
           vaultInput.setText(score);
            return false;
    }});
    vaultInput.setPosition(x1, y1);
    vault1Button.setPosition(x2, y2);
    stage.addActor(vaultInput);
    stage.addActor(vault1Button);
}

Everything added to the stage will be drawn when calling stage.draw()

調用stage.draw()時將繪制添加到舞台的所有內容

#2


0  

The value of the vault1 field is still null when show() is called. So your actual button that you added your listener to has zero size and nothing to draw.

調用show()時,vault1字段的值仍為null。因此,您添加聽眾的實際按鈕大小為零,無需繪制。

Also, since render() is called continuously in the game loop, you are creating new sprites to draw on every frame and reassigning the vault1 variable every frame.

此外,由於在游戲循環中連續調用render(),因此您將創建新的精靈以在每個幀上繪制並每幀重新分配vault1變量。

You need to create your image for the button in show() and add the button to the stage. Then you only need to draw the stage.

您需要在show()中為按鈕創建圖像,並將按鈕添加到舞台。那你只需要繪制舞台。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2018/01/25/8e9a008611bce1d3a6cd8585984cdecb.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com