libgdx clickable image not working

时间秒杀一切 提交于 2019-12-13 16:58:52

问题


I have a menu screen in libgdx and I had a text button that started a new game like this.

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?


回答1:


The problem here is that Image does not fire a changed(...) event anymore. 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. 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.

Change it to a ClickListener and use the clicked(...) method instead. This event should be fired by all actors in the scene2d.ui package.




回答2:


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  

    }

});


来源:https://stackoverflow.com/questions/23574821/libgdx-clickable-image-not-working

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!