问题描述
我在libgdx中有一个菜单屏幕,并且有一个文本按钮可以启动像这样的新游戏.
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);
和
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?
推荐答案
此处的问题是Image
不再触发changed(...)
事件.仅当状态从单击"更改为未单击"以及相反时,您才使用此TextButton
触发此事件.正如JavaDoc所述,它在其他情况下也可以触发,因为它是一种通用"事件,但因参与者而异.
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.
将其更改为ClickListener
并改用clicked(...)
方法.此事件应由scene2d.ui
程序包中的所有参与者触发.
Change it to a ClickListener
and use the clicked(...)
method instead. This event should be fired by all actors in the scene2d.ui
package.
这篇关于libgdx可点击的图片不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!