如何在libgdx scene2d上拖放actor?

时间:2013-04-29 20:35:41

标签: java android libgdx

我正在使用libGDX开发一款游戏,我想知道如何拖放一个Actor。我已经完成了我的舞台并吸引了演员,但我不知道如何触发这个事件。

请尽量帮助我使用自己的架构。

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

3 个答案:

答案 0 :(得分:11)

看一下libgdx示例中的Example。以下是libgdx测试类的拖放测试:DragAndDropTest

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

答案 1 :(得分:9)

如果您不想使用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);
    }
});

修改:方法drag代替touchDragged

答案 2 :(得分:2)

在主游戏屏幕类中添加多路复用器,以便您可以访问来自不同类的事件:

private InputMultiplexer inputMultiplexer = new InputMultiplexer(this); 

以gamecreen构造函数添加为例:

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

在使用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);

        }

    }));
相关问题