【问题标题】:LibGdx: How to implement gestures on ActorLibGdx:如何在 Actor 上实现手势
【发布时间】:2014-08-30 09:13:01
【问题描述】:

我正在尝试使用 ActorGestureListener 在 LibGdx 中的 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();
        }
    }

【问题讨论】:

  • 你是通过 setBounds 还是 setWidth/Height 给 Circle 一个尺寸?

标签: libgdx


【解决方案1】:
    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);

【讨论】:

    猜你喜欢
    • 2018-01-21
    • 2012-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多