rsa*_*san 2 java android game-engine libgdx
我正在测试libgdx,但我陷入了用户输入处理.
我的第一次尝试是直接从渲染方法使用Gdx.input,但我觉得重新发明轮子,因为当我得到触摸事件时,我正在编写大量代码来检测输入区域.
我几乎肯定应该使用Actor类更好的方法,但是因为事件永远不会发生,所以我必须做错事.
这是我的代码:
...
Texture texture = new Texture(Gdx.files.internal("assets/sprite-sheet.png"));
singlePlayerButton = new Image("SinglePlayerButton", new TextureRegion(texture,0,0,50,50)){
@Override
public boolean touchDown(float x, float y, int pointer) {
// TODO Auto-generated method stub
System.out.println("touch down");
return super.touchDown(x, y, pointer);
}
@Override
public void touchUp(float x, float y, int pointer) {
System.out.println("touch up");
}
};
stage.addActor(singlePlayerButton);
...
public void render(float delta) {
// Clear the screen
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
stage.draw();
spriteBatch.end();
}
Run Code Online (Sandbox Code Playgroud)
图像显示效果不错,但无论我点击多少次都没关系,我从未获得过该事件.我错过了什么?注册活动?我在Stage或Actor类中找不到任何addTouchListener()方法.
谢谢!
Lud*_*vik 16
您必须使用libGDX注册所有输入处理器.舞台实现InputProcessor,所以你必须注册它:
@Override
public void create() {
//... initialization
Gdx.input.setInputProcessor(stage);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5137 次 |
| 最近记录: |