我遇到一个问题,即当我按任意键时,keyDown
事件永远不会在Actor中触发。但是,touchDown
(用于鼠标点击)可以正常工作。
演员代码:
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添加到Stage
:
@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);
}
欢迎任何建议。
答案 0 :(得分:2)
只有具有键盘焦点的actor才会收到键盘事件。
要设置哪个actor具有键盘焦点,您可以使用以下...
`Stage.setKeyboardFocus(Actor actor);`
从那时起,键盘事件将被传递给指定的actor。