how to drag and drop actors on libgdx scene2d?

前端 未结 3 852
鱼传尺愫
鱼传尺愫 2021-02-02 16:04

I\'m developing a game using libGDX and I would like to know how I can drag and drop an Actor. I\'ve made my stage and drawn the actor, but I don\'t know how to trigger that eve

相关标签:
3条回答
  • 2021-02-02 16:52

    If you don't want to use DragAndDrop class, you can use this:

    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);
        }
    });
    

    Edit: method drag instead touchDragged

    0 讨论(0)
  • 2021-02-02 16:58

    In your main gamescreen class add a multiplexer so you can access events from different classes:

    private InputMultiplexer inputMultiplexer = new InputMultiplexer(this); 
    

    After the gamescreen constructor add as an example:

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

    In your class that is using the actors use a DragListener as and example:

    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);
    
            }
    
        }));
    
    0 讨论(0)
  • 2021-02-02 17:01

    Take a look at the Example in the libgdx examples. Here is the drag and drop test from the libgdx test classes: DragAndDropTest

    If you just want to drag/slide your Actor around you need to add a GestureListener to it and pass your Stage to the Inputprocessor like this:Gdx.input.setInputProcessor(stage);. Here is the GestureDetectorTest from libgdx. For drag events its the Flinglistener.

    0 讨论(0)
提交回复
热议问题