سؤال

i am trying to escalate touchDown and touchUp events from an actor Object to my applicationListener class. To do so, i called fire(event); in the InputListener of my Actor

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 + ")");
        }
});

To handle the event in my ApplicationListener class (which contains the stage of the actors), i added an InputListener to the stage

        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.");
                }
        });

However when i touch my actor, i get a StackOverflowError as well as a ton of exceptions from several InputListeners (i assume that the event is not stopped properly and propagated to all actors in my scene). What am i missing here?

Also after firing the event i cant cast event.getTarget() (which is an Actor) to my Actor-Subclass anymore, which works just fine if i do this in the ActorSubclass itself. Meaning the following code creates an error when used in the ApplicationListener, but works in the MyActor class:

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

Since the target is in fact a MyActor Object, how can i access it not only as Actor, but as MyActor?

هل كانت مفيدة؟

المحلول

As @chase said you are recursivelly calling the fire(even) method, as you can only set 1 InputProcessor (your stage) and so always the same method recieves the event.
You should instead use a InputMultiplexer:

  1. add the Stage and your ApplicationListener as InputProcessor to the InputMultiplexer
  2. Set the InputMulitplexer as the InputProcessor by calling Gdx.input.setInputProcessor(multiplexer);.

The InputMultiplexer will then give the event to the first InputProcessor and if it returns false, it will send the event to the next one.
So in your Stage you just need to return false.
But i don't understand why you are adding your Stage as a InputProcessor if you don't want it to handle inputs...
Anyways in scene2d you don't need to add an InputProcessor to the stage as it allready has one. Also the Actor allready has a boolean touchDown(float x, float y, int pointer) and other helpful methods.
A useful link: Scene2D wiki article

نصائح أخرى

Aren't you just making a recursive call at this point by refiring the event every time you handle it?

Why not just return false from your listener?

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top