如何在 libgdx scene2d 上拖放演员?

2022-01-12 00:00:00 android java libgdx

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

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

推荐答案

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

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

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

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.

相关文章