2

I have a stage and the actor on it with size 200x200. I need the actor catch touch events to make some calculations. But the problem is that touch events triggers only when touched over the actors bound. How can i get the event of touching any area of the screen, but handled inside the actor`s class?

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);
        }
    });
}
anmig
  • 23
  • 4

1 Answers1

2

Add listener on stage instead of any particular 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);
            }
        });

EDIT

Fire event on all required Actor and handle in MyActor listener tocuhDown(..) method

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 class

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;
        }
    });
  }
}
Abhishek Aryan
  • 19,936
  • 8
  • 46
  • 65
  • Thanks. But in such case i have to recalculate the touch coords in all actors by myself. I mean in MyActor i must convert scene touch X to MyActor touch X. Thats why i was asking is there any way to get the coords outside the actor but handle within actor. – anmig Jul 06 '17 at 04:19
  • `i must convert scene touch X to MyActor touch X` ? – Abhishek Aryan Jul 06 '17 at 05:53
  • I mean that when i add input listener to the stage - in touchDown method i get X and Y of the touch according to stage, but not to the actor's coord system! E.g. in stage coords (x = 10, y = 10), that means in actor's coord sys it will be e.g. (x = -300, y = 125). So if i have a complicated ierarchy of actors it is hard to handle touch events only in stage touchDown. Because i must convert (x = 10, y = 10) of Stage to (x = -300, y = 125) of MyActor! So i want to handle events in actor`s classes, because the coords come to them allredy converted to their own coord sys. – anmig Jul 06 '17 at 06:12
  • why you want to convert stage x, y to actor cords. what if you've 5 `MyActor` on stage and you touched outside of all actor bound. which actor will handle your touch ? – Abhishek Aryan Jul 06 '17 at 06:27
  • All of them. It is the same as the touch would be INSIDE of 5 MyActor instances. And everyone will get its own x and y (in their own coordinate system) and use them to calculate some game logic. LibGDX makes very good job with converting coords from parent to child, and i wanted to use it. But as far - as i understand that it is unreachable. – anmig Jul 06 '17 at 06:50
  • I am sorry, but your code does not work. The event ("MyActor","touched") triggers only when the touch is over the MyActor bounds area, but not anywhere on the stage. – anmig Jul 06 '17 at 11:36
  • I reviewed my code, working fine as expected, so please review your code once or even make a simple test code. use `stage.setDebugAll(true);` for bounds – Abhishek Aryan Jul 06 '17 at 11:52