【问题标题】:how to drag and drop actors on libgdx scene2d?如何在 libgdx scene2d 上拖放演员?
【发布时间】:2013-04-23 15:46:19
【问题描述】:

我正在使用 libGDX 开发游戏,我想知道如何拖放 Actor。我已经搭建好舞台并绘制了演员,但我不知道如何触发该事件。

请尝试帮助我使用我自己的架构。

public class MyGame implements ApplicationListener 
{
    Stage stage;
    Texture texture;
    Image actor;

    @Override
    public void create() 
    {       
        texture = new Texture(Gdx.files.internal("actor.png"));
        Gdx.input.setInputProcessor(stage);
        stage = new Stage(512f,512f,true);

        actor = new Image(texture);
        stage.addActor(actor);
    }

    @Override
    public void render() 
    {       
        Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
        stage.draw();
    }
}

【问题讨论】:

    标签: java android libgdx


    【解决方案1】:

    查看 libgdx 示例中的示例。这是来自 libgdx 测试类的拖放测试:DragAndDropTest

    如果您只想拖动/滑动您的 Actor,您需要向其添加一个 GestureListener 并将您的 Stage 传递给 Inputprocessor,如下所示:Gdx.input.setInputProcessor(stage);。 这是来自 libgdx 的GestureDetectorTest。 对于拖动事件,它是 Flinglistener。

    【讨论】:

      【解决方案2】:

      如果你不想使用DragAndDrop 类,你可以使用这个:

      actor.addListener(new DragListener() {
          public void drag(InputEvent event, float x, float y, int pointer) {
              actor.moveBy(x - actor.getWidth() / 2, y - actor.getHeight() / 2);
          }
      });
      

      编辑:方法drag 而不是touchDragged

      【讨论】:

        【解决方案3】:

        在您的主游戏屏幕类中添加一个多路复用器,以便您可以访问来自不同类的事件:

        private InputMultiplexer inputMultiplexer = new InputMultiplexer(this); 
        

        gamescreen构造函数后添加为例:

        inputMultiplexer = new InputMultiplexer(this);      
        inputMultiplexer.addProcessor(1, renderer3d.controller3d);  
        inputMultiplexer.addProcessor(2, renderer.controller2d);
        inputMultiplexer.addProcessor(3, renderer3d.stage);
        Gdx.input.setInputProcessor(inputMultiplexer);
        

        在您使用演员的班级中,使用 DragListener 作为示例:

        Actor.addListener((new DragListener() {
            public void touchDragged (InputEvent event, float x, float y, int pointer) {
                    // example code below for origin and position
                    Actor.setOrigin(Gdx.input.getX(), Gdx.input.getY());
                    Actor.setPosition(x, y);
                    System.out.println("touchdragged" + x + ", " + y);
        
                }
        
            }));
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-10-04
          • 2013-03-26
          • 2020-07-13
          • 2016-05-28
          相关资源
          最近更新 更多