how to drag and drop actors on libgdx scene2d?

前端 未结 3 853
鱼传尺愫
鱼传尺愫 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: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);
    
            }
    
        }));
    

提交回复
热议问题