scene2d:如何在场景/ root上触发(事件)和处理?

时间:2014-03-27 22:02:18

标签: java android libgdx scene2d

我正在尝试将touchDown和touchUp事件从actor Object升级到我的applicationListener类。为此,我打电话给火(事件);在我的演员的InputListener中

this.addListener(new InputListener(){
        public boolean touchDown(InputEvent event, float x, float y, int pointer, int buttons){
            Gdx.app.log("Example", "touch started at (" + x + ", " + y + ")");
            fire(event);
            return true;
        }
        public void touchUp(InputEvent event, float x, float y, int pointer, int buttons){
            Gdx.app.log("Example", "touch ended at (" + x + ", " + y + ")");
        }
});

要在我的ApplicationListener类(包含actor的舞台)中处理事件,我在舞台上添加了一个InputListener

        Gdx.input.setInputProcessor(stage);
        stage.addListener(new InputListener(){
            public boolean touchDown(InputEvent event, float x, float y, int pointer, int buttons){
                Gdx.app.log("FIRE!!!", "I CAUGHT A FIRED EVENT!");
                event.stop();
                return true;
            }
            public void touchUp(InputEvent event, float x, float y, int pointer, int buttons){
                Gdx.app.log("FIRE!!!", "the fired event even touchupped.");
                }
        });

然而,当我触摸我的actor时,我得到一个StackOverflowError以及来自几个InputListeners的大量异常(我假设事件没有正确停止并传播到我场景中的所有actor)。我在这里缺少什么?

在触发事件之后,我不能再将event.getTarget()(这是一个Actor)转换为我的Actor-Subclass,如果我在ActorSubclass本身中执行此操作,则效果很好。这意味着以下代码在ApplicationListener中使用时会产生错误,但在MyActor类中起作用:

MyActor actor = (MyActor)event.getTarget();

由于目标实际上是MyActor对象,我如何才能将其作为Actor访问,而不是MyActor?

2 个答案:

答案 0 :(得分:0)

你只是在每次处理事件时通过重新启动事件来进行递归调用吗?

为什么不从听众那里回复假?

答案 1 :(得分:0)

由于@chase说你递归调用fire(even)方法,因为你只能设置1个InputProcessor(你的stage),所以总是用相同的方法接收事件。
您应该使用InputMultiplexer

  1. StageApplicationListener InputProcessor添加到InputMultiplexer
  2. 通过调用InputMulitplexerInputProcessor设置为Gdx.input.setInputProcessor(multiplexer);
  3. 然后InputMultiplexer会将事件提供给第一个InputProcessor,如果返回false,则会将事件发送到下一个事件。
    所以在你的Stage中,你只需要返回虚假 但是,如果您不希望它处理输入,我不明白为什么要将Stage添加为InputProcessor ... 无论如何,在scene2d中你不需要在舞台上添加InputProcessor,因为它已经有了一个。{1}}。此外,Actor已经有boolean touchDown(float x, float y, int pointer)和其他有用的方法 一个有用的链接:Scene2D wiki article