gpt4 book ai didi

java - InputListener 不适用于 OrthographicCamera 和 Actors

转载 作者:行者123 更新时间:2023-11-30 11:10:15 26 4
gpt4 key购买 nike

我有一个带有 OrthographicCamera 的舞台,还有一个带有 addListener setter (来自 actor 类)的 InputListener 的 Actor。问题是 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){
...
}
}

最佳答案

仅将舞台注册为 InputProcessor 是不够的。还需要在每一帧通过stage.act()触发Stage的事件处理。

此外,您需要在发生调整大小事件时正确更新舞台的 Viewport。这可以通过 stage.getViewport().update(width, height, true) 完成。否则,舞台将根据对屏幕尺寸的错误假设来处理事件,并且还可能以您不希望的方式渲染您的舞台。 true 很重要,因为它还会将相机置于新屏幕尺寸的中心,这在 UI 的情况下是必需的。

关于java - InputListener 不适用于 OrthographicCamera 和 Actors,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27776440/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com