LibGdx: How to Implement Gestures on Actors

I am trying to implement gestures on Actors in LibGdx using ActorGestureListener. The following code draws the actor, but the pan method is not called. Did I miss something?

I am using LibGdx 1.3.1.

   public class MyApp extends ApplicationAdapter {

        private Stage stage;

        @Override
        public void create() {
            stage = new Stage(new ExtendViewport(Gdx.graphics.getWidth(), Gdx.graphics.getHeight()));
            Gdx.input.setInputProcessor(stage);

            final Shape circle = new Circle(); // Actor that draws a texture
            stage.addActor(circle);

            circle.addListener(new ActorGestureListener(){
                public void pan (InputEvent event, float x, float y, float deltaX, float deltaY) {
                    Gdx.app.log("", "pan");
                    event.getTarget().moveBy(deltaX, deltaY);
                }
            });

            circle.setTouchable(Touchable.enabled);
        }


        @Override
        public void render() {
            Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
            stage.act(Gdx.graphics.getDeltaTime());
            stage.draw();
        }
    }

      

+3


source to share


1 answer


    If you inherit from Actor, you need to set the bounds or it will not be click/touch-able!.
    Simply set the bounds to match the texture your Actor contains.

    //add this Set bounds the x, y, width, and height
                circle.setBounds(0, 0,texture.getWidth(),
texture.getHeight());
    //add this Sets the origin position which is relative to the actor bottom left corner.
                circle.setOrigin(0, 0);   


            stage.addActor(circle);

      



0


source







All Articles