简体   繁体   English

libgdx可点击的图片不起作用

[英]libgdx clickable image not working

I have a menu screen in libgdx and I had a text button that started a new game like this. 我在libgdx中有一个菜单屏幕,并且有一个文本按钮可以启动像这样的新游戏。

textButton.addListener(new ChangeListener() {
    public void changed (ChangeEvent event, Actor actor) {
        g.setScreen( new level1(g));
    }
});

It looked like crap so I changed it to an image. 看起来像胡扯,所以我将其更改为图像。

playbuttontexture = new Texture(Gdx.files.internal("data/playbutton.png"));
playbuttontexture.setFilter(TextureFilter.Linear, TextureFilter.Linear);

TextureRegion playbuttonregion = new TextureRegion(playbuttontexture, 0, 0, 512, 256);//powers of 2

playbutton = new Image(playbuttonregion);
playbutton.setSize(512,256);
playbutton.setBounds(width/2-playbutton.getWidth()/2, height/2-playbutton.getHeight()/2, 512, 256);
//playbutton.setOrigin(playbutton.getWidth()/2, playbutton.getHeight()/2);
playbutton.setPosition(width/2-playbutton.getWidth()/2, height/2-playbutton.getHeight()/2);

and

playbutton.addListener(new ChangeListener() {
    public void changed (ChangeEvent event, Actor actor) {
        g.setScreen( new level1(g));
    }
});

Now when I click it nothing happens? 现在,当我单击它时,什么都没有发生? What am I doing wrong? 我究竟做错了什么?

The problem here is that Image does not fire a changed(...) event anymore. 这里的问题是Image不再触发已changed(...)事件。 This event is only fired by the TextButton you've used before when the status changes from clicked to not-clicked and the other way around. 仅当状态从单击更改为未单击以及TextButton时,此事件才由您之前使用的TextButton触发。 It can also be fired in other cases, since it is kind of a "generic" event, as the JavaDoc states, but that varies from actor to actor. 正如JavaDoc所述,它在其他情况下也可以触发,因为它是一种“通用”事件,但因参与者而异。

Change it to a ClickListener and use the clicked(...) method instead. 将其更改为ClickListener并改用clicked(...)方法。 This event should be fired by all actors in the scene2d.ui package. 此事件应由scene2d.ui程序包中的所有参与者scene2d.ui

for me this is what it looks like when I implemented the code ( I was having a similar issue. noone did a great job at pointing me to the right places to look. ) 对我来说,这就是我实现代码时的样子(我遇到了类似的问题。没有人在指出正确的位置方面做得很好。)

playbutton.addListener( new ClickListener(){
    @Override
    public void clicked (InputEvent event, float x, float y) {
    //your code to do stuff when the button is clicked  

    }

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM