我有一个带有OrthographicCamera的舞台,我还有一个带有InputListener的Actor,由addListener(来自actor类)设置。问题是演员没有处理输入,但是如果在我的屏幕中我删除了OrthographicCamera,则Actor会处理输入,因此,使用OrthographicCamera Actor不会处理输入但是如果我删除了OrthographicCamera它会起作用。
有什么建议吗?
我有以下代码
public class Test implements Screen {
private Game game;
private Stage stage;
private MemoryActor actor;
private AssetManager manager;
private boolean loaded = false;
float width, height;
private OrthographicCamera camera;
public Test(Game game){
this.game = game;
stage = new Stage();
manager = new AssetManager();
manager.load("img.png",Texture.class);
manager.load("img1.png",Texture.class);
InputMultiplexer im = new InputMultiplexer();
im.addProcessor(stage);
Gdx.input.setInputProcessor(im);
height = Gdx.graphics.getHeight();
width = Gdx.graphics.getWidth();
camera = new OrthographicCamera(width, height);
camera.position.set(((width / 2)), ((height / 2)), 0);
camera.update();
stage.setViewport(new ExtendViewport(300,300, camera));
}
public void createActor(){
Texture back = manager.get("img.png", Texture.class);
actor = new MemoryActor(manager.get("img1.png", Texture.class), back,0,0,50,50);
actor.setInputListener(new InputListener(){
@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
System.out.println("down");
return true;
}
@Override
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
System.out.println("up");
}
});
stage.addActor(actor);
}
@Override
public void render(float delta) {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
if (manager.update()){
if (!loaded){
createActor();
loaded = true;
}
}
stage.draw();
}
@Override
public void resize(int width, int height) {
}
@Override
public void show() {
}
@Override
public void hide() {
}
@Override
public void pause() {
}
@Override
public void resume() {
}
@Override
public void dispose() {
}
}
和MemoryActor:
public class MemoryActor extends Actor {
...
public MemoryActor(){}
public MemoryActor(Texture texture, Texture texBack, float x, float y, float width, float height){
...
}
public void setInputListener(InputListener il){
addListener(il);
}
@Override
public void draw(Batch batch, float alpha){
...
}
}
答案 0 :(得分:1)
仅仅将舞台注册为InputProcessor
是不够的。您还需要在每个帧中通过Stage
触发stage.act()
的事件处理。
此外,您需要在发生调整大小事件时正确更新舞台的Viewport
。这可以通过stage.getViewport().update(width, height, true)
完成。否则,舞台将根据有关屏幕尺寸的错误假设处理事件,也可能使您的舞台不是您想要的方式。 true
很重要,因为它还会将相机置于新的屏幕尺寸中,这在UI的情况下是必需的。