Scene2d how to handle touched actor?(LibGDX)

后端 未结 3 1937
轮回少年
轮回少年 2020-12-21 02:44

I have a problem with using scene2d in libgdx. I can\'t find anywhere a method that allows me to check wheter the actor is touched or not. I can only find methods that told

3条回答
  •  清酒与你
    2020-12-21 03:01

    I had the similar problem and moreover I needed to know the current coordinates; So I resolved the issue something like this:

    At first we extend the standard listener:

    class MyClickListener extends ClickListener {
        float x, y = 0;
    
        @Override
        public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
            this.x = x;
            this.y = y;
            return super.touchDown(event, x, y, pointer, button);
        }
    
        @Override
        public void touchDragged(InputEvent event, float x, float y, int pointer) {
            this.x = x;
            this.y = y;
            super.touchDragged(event, x, y, pointer);
        }
    }
    

    Then add an instance to the Actor:

    class MyActor extends Actor {
        private final MyClickListener listener = new MyClickListener();
        MyActor() {
            addListener(listener);
        }
        ...
    }
    

    And in a draw (act) method use the following:

    if (listener.getPressedButton() >= 0)
        System.out.println(listener.x + "; " + listener.y);
    

提交回复
热议问题