[英]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.