java InputListener不适用于正交摄影机和角色
我有一个带有正交摄影机的舞台,还有一个演员,他有addListener(来自Actor类)提供的InputListener setter。问题是,演员不处理输入,但如果在我的屏幕中删除正交摄影机,演员将处理输入,因此,对于正交摄影机,演员不处理输入,但如果删除正交摄影机,它会工作
有什么建议吗
我有以下代码
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() {
}
}
和记忆因子:
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){
...
}
}
# 1 楼答案
仅仅将stage注册为
InputProcessor
是不够的。您还需要在每个帧中通过stage.act()
触发Stage
的事件处理此外,当发生调整大小事件时,您需要正确地更新stage的
Viewport
。这可以通过stage.getViewport().update(width, height, true)
实现。否则,该阶段将根据屏幕大小的错误假设处理事件,并可能使您的阶段不是您想要的方式。true
很重要,因为它还将使相机在新的屏幕尺寸上居中,这对于UI来说是必要的