LibGdx:如何在Actor上实现手势

时间:2014-08-30 09:13:01

标签: libgdx

我正在尝试使用ActorGestureListener在LibGdx中的Actors上实现手势。以下代码绘制actor,但不调用pan方法。我错过了什么吗?

我正在使用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();
        }
    }

1 个答案:

答案 0 :(得分:0)

    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's bottom left corner.
                circle.setOrigin(0, 0);   


            stage.addActor(circle);