简体   繁体   English

如何在libgdx scene2d上拖放actor?

[英]how to drag and drop actors on libgdx scene2d?

I'm developing a game using libGDX and I would like to know how I can drag and drop an Actor. 我正在使用libGDX开发游戏,我想知道如何拖放一个Actor。 I've made my stage and drawn the actor, but I don't know how to trigger that event. 我已经完成了我的舞台并吸引了演员,但我不知道如何触发该事件。

Please try to help me using my own architecture. 请尝试帮助我使用自己的架构。

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

Take a look at the Example in the libgdx examples. 看一下libgdx示例中的Example。 Here is the drag and drop test from the libgdx test classes: DragAndDropTest 以下是libgdx测试类的拖放测试: 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); 如果你只想拖动/滑动你的Actor,你需要为它添加一个GestureListener并将你的舞台传递给Inputprocessor,如下所示: Gdx.input.setInputProcessor(stage); . Here is the GestureDetectorTest from libgdx. 这是libgdx的GestureDetectorTest。 For drag events its the Flinglistener. 对于拖动事件,它是Flinglistener。

If you don't want to use DragAndDrop class, you can use this: 如果您不想使用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);
    }
});

Edit: method drag instead touchDragged 编辑:方法drag而不是touchDragged

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: 以gamecreen构造函数添加为例:

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的类中使用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);

        }

    }));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM