gpt4 book ai didi

java - Actor 的输入监听器

转载 作者:行者123 更新时间:2023-11-30 03:24:52 24 4
gpt4 key购买 nike

我遇到一个问题,当我按任意键时,Actor 中的 keyDown 事件永远不会触发。不过,touchDown(用于鼠标点击)可以工作。

Actor 代码:

public class MapActor extends Actor {
private TiledMap map;
private OrthogonalTiledMapRenderer renderer;
private int directions = 0;
private OrthographicCamera camera;

public MapActor(String pathToMap, OrthographicCamera camera) {
TmxMapLoader loader = new TmxMapLoader();
map = loader.load(pathToMap);
renderer = new OrthogonalTiledMapRenderer(map);
this.camera = camera;

this.setBounds(0, 0, 500, 500);
this.addListener(new InputListener() {
// a - 29, w - 51, d - 32, s - 47
@Override
public boolean keyDown(InputEvent event, int keycode) {
System.out.println("Test");
return true;
}


@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
Gdx.app.log("Touch", "touch down");
return true;
}
});
}

@Override
public void act(float delta) {
}

@Override
public void draw(Batch batch, float parentAlpha) {
super.draw(batch, parentAlpha);
batch.end();
renderer.setView(camera);
renderer.render();
batch.begin();
}

public void dispose() {
renderer.dispose();
map.dispose();
}
}

以下是我将 Actor 添加到舞台的方法:

@Override
public void create () {
stage = new Stage(new ScreenViewport());
Gdx.input.setInputProcessor(stage);

MapActor mapActor = new MapActor("maps/testmap.tmx", (OrthographicCamera) stage.getCamera());
stage.addActor(mapActor);

}

欢迎任何建议。

最佳答案

只有拥有键盘焦点的 actor 才会收到键盘事件。

要设置哪个 Actor 拥有键盘焦点,您可以使用以下命令...

    `Stage.setKeyboardFocus(Actor actor);`

从此以后,键盘事件将被传递给指定的actor。

关于java - Actor 的输入监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30490832/

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