Input Stage.addListener在libgdx中不工作
我只是想在libgdx中的stage中添加一个click侦听器,但它不起作用,这是我的代码Input Stage.addListener在libgdx中不工作,input,libgdx,Input,Libgdx,我只是想在libgdx中的stage中添加一个click侦听器,但它不起作用,这是我的代码 stage.addListener(new InputListener() { @Override public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) { Gdx.app.log("CLICK", "LISTENER");
stage.addListener(new InputListener() {
@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
Gdx.app.log("CLICK", "LISTENER");
parallaxBackground.reverse();
return super.touchDown(event, x, y, pointer, button);
}
});
这是我的游戏主屏幕,它继承了一个抽象屏幕,调用
Gdx.input.setInputProcessor(stage);
在抽象类中,我也叫
stage.act(delta);
我非常明确地从子类调用了super.render()
这是什么原因造成的
编辑
我不知道这是否重要,但我的舞台上没有演员,但我只想简单地确认一下点击。由于您没有在舞台上添加演员,您的舞台没有实质内容,因此无法触摸 您可以添加并创建一个与屏幕大小相同且与屏幕对齐的演员,以完成您正在尝试的操作。类似这样(未经测试):
你可以先添加这个演员,这样其他演员就可以在截取触球时获得第一次破解,这是一个退路。下面是我如何解决这个问题的:
- 在Photoshop中,我创建了一个100x100px的图像,只放了一层,并将该层的不透明度设置为1%,我还删除了背景(这是一个完全透明的图像),并将其保存为.png以用作纹理
- 我创建了一个
,将其命名为Actor
,并为其绘制纹理BgActor
演员的样子:
BgActor
类:
public class BgActor extends Actor {
private float x, y, width, height;
private Texture texture = new Texture(Gdx.files.internal("images/transparent-bg.png"));
public BgActor(float x, float y, float width, float height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
setTouchable(Touchable.enabled);
}
@Override
public void draw(Batch batch, float parentAlpha) {
setBounds(x, y, width, height);
batch.draw(texture, x, y, width, height);
}
public void dispose() {
texture.dispose();
}
}
实现(我使用了您的侦听器):
不要忘记在屏幕的hide()方法中处理bgActor
祝你好运
public class BgActor extends Actor {
private float x, y, width, height;
private Texture texture = new Texture(Gdx.files.internal("images/transparent-bg.png"));
public BgActor(float x, float y, float width, float height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
setTouchable(Touchable.enabled);
}
@Override
public void draw(Batch batch, float parentAlpha) {
setBounds(x, y, width, height);
batch.draw(texture, x, y, width, height);
}
public void dispose() {
texture.dispose();
}
}
BgActor bgActor = new BgActor(0, 0, stage.getWidth(), stage.getHeight);
bgActor.addListener(new InputListener() {
@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
Gdx.app.log("CLICK", "LISTENER");
parallaxBackground.reverse();
return super.touchDown(event, x, y, pointer, button);
}
});
......
stage.addActor(bgActor);