簡體   English   中英

如何在libgdx scene2d上拖放actor?

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

我正在使用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();
    }
}

看一下libgdx示例中的Example。 以下是libgdx測試類的拖放測試: DragAndDropTest

如果你只想拖動/滑動你的Actor,你需要為它添加一個GestureListener並將你的舞台傳遞給Inputprocessor,如下所示: Gdx.input.setInputProcessor(stage); 這是libgdx的GestureDetectorTest。 對於拖動事件,它是Flinglistener。

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

在主游戲屏幕類中添加多路復用器,以便您可以訪問來自不同類的事件:

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

        }

    }));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM