我有一个舞台和演员,大小为200x200。我需要演员抓住触摸事件来进行一些计算。但问题是触摸事件只有在触及actor绑定时触发。我怎样才能触摸屏幕的任何区域,但是在演员的课堂内处理?
public class MyActor extends Actor {
public MyActor() {
setBounds(100f, 100f, 200f, 200f);
addListener(new InputListener() {
@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
return super.touchDown(event, x, y, pointer, button);
}
@Override
public void touchDragged(InputEvent event, float x, float y, int pointer) {
super.touchDragged(event, x, y, pointer);
}
@Override
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
super.touchUp(event, x, y, pointer, button);
}
});
}
答案 0 :(得分:1)
在舞台上添加侦听器而不是任何特定的Actor
stage.addListener(new InputListener(){
@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
Gdx.app.log("Stage Listener","inside touch down");
return super.touchDown(event, x, y, pointer, button);
}
@Override
public void touchDragged(InputEvent event, float x, float y, int pointer) {
super.touchDragged(event, x, y, pointer);
}
@Override
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
super.touchUp(event, x, y, pointer, button);
}
});
修改强>
在MyActor
侦听器tocuhDown(..)
方法
stage.addListener(new InputListener(){
@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
for(Actor actor:stage.getActors()){
if(actor.getName().equals("MyActor")) {
InputEvent myEvent=new InputEvent();
myEvent.setType(InputEvent.Type.touchDown);
myEvent.setStage(stage);
myEvent.setPointer(pointer);
myEvent.setButton(button);
myEvent.setStageX(x);
myEvent.setStageY(y);
actor.fire(myEvent);
}
}
return super.touchDown(event,x,y,pointer,button);
}
});
MyActor
上课
public class MyActor extends Actor {
public MyActor(){
setBounds(100f, 100f, 200f, 200f);
setName("MyActor");
addListener(new InputListener() {
@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
Gdx.app.log("MyActor","touched");
event.stop();
return false;
}
});
}
}