LibGdx Stage / Actor InputListener(InputListener的适当区域)

时间:2013-07-18 03:07:07

标签: libgdx

我有一个扩展Actor的类Bubble。

public Bubble(MyGdxGame game,Texture texture){
    this.game=game;
    setPosition(0,0);
    setSize(32,32);

    gameObject=new GameObject("","bubble");
    direction=new MovementDirection();
    sprite=new Sprite(texture);

    setTouchable(Touchable.enabled);
    setWidth(sprite.getWidth());
    setHeight(sprite.getHeight());
    setBounds(0,0,sprite.getWidth(),sprite.getHeight());

    addListener(new InputListener() {
        public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
            Gdx.app.log("BUBBLE", "touchdown");
            return true;  // must return true for touchUp event to occur
        }
        public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
            Gdx.app.log("BUBBLE", "touchup");
        }
    });
}

这是在实现Screen

的类中
public void show() {
    // TODO Auto-generated method stub
    super.show();

    //2 bubbles test
    gameStage=new Stage(MyGdxGame.WIDTH,MyGdxGame.HEIGHT,true);
    Gdx.input.setInputProcessor(gameStage);

    for (int i=0; i<10; i++){
        Bubble b=new Bubble(game,Assets.bubbleTexture);
        b.randomize();
        gameStage.addActor(b);
    }

    //if (bubbleList==null)
    //  createBubbles();

}

我是否通过添加监听器@气泡级别来解决这个问题? (似乎为我生成的每个气泡创建一个InputListener有点疯狂)。

根据:http://libgdx.l33tlabs.org/docs/api/com/badlogic/gdx/scenes/scene2d/Actor.html Actor有一个touchUp()和touchDown事件 - 但是当我试图覆盖它们时会抱怨(这让我相信它们不存在)。我认为压倒这些将是一种更好的方法

1 个答案:

答案 0 :(得分:4)

您链接的文档已过时。 这些方法已被弃用并删除,转而使用InputListener s。

在您的示例中,如果您要对InputListener类(Actor)的所有实例使用相同的Bubble实例,那么您可以实现InputListener来引用使用inputEvent.getRelatedActor()对Actor类实例进行实例化,然后将InputListener实例化为Bubble的静态成员,并将其在构造函​​数中传递给addListener

class Bubble extends Actor{
   private static InputListener bubbleListener= new InputListener() {
      public boolean touchDown (InputEvent event, float x, float y, int pointer, int button)        {
         Bubble b = (Bubble) event.getRelatedActor();
         b.doSomething();
         ...
         return true; //or false
      }
   }
   public Bubble(){
       addListener(bubbleListener);
       ...
   }
   ...

}
相关问题