java 如何在 libgdx scene2d 上拖放演员?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16287759/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 22:31:40  来源:igfitidea点击:

how to drag and drop actors on libgdx scene2d?

javaandroidlibgdx

提问by Juan Mitchell

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.

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

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

采纳答案by BennX

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

查看 libgdx 示例中的示例。这是来自 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);. Here is the GestureDetectorTestfrom libgdx. For drag events its the Flinglistener.

如果你只是想拖/在你身边需要一个GestureListener添加到它,并通过你的舞台在Inputprocessor这样的滑动演员:Gdx.input.setInputProcessor(stage);。这里是GestureDetectorTest从libgdx。对于拖动事件,它是 Flinglistener。

回答by pablo2303

If you don't want to use DragAndDropclass, 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 draginstead touchDragged

编辑:方法drag代替touchDragged

回答by GothicFan

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:

在 gamescreen 构造函数后添加作为示例:

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:

在您使用演员的课程中,使用 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);

        }

    }));