准确移动演员 LibGDX Scene2d
Move Actors accuratley LibGDX Scene2d
所以我有一个演员,它是一个精灵,设置在屏幕视口舞台上。我想要做的是能够触摸演员,然后触摸屏幕上的一个点,它会流畅地移动。目前,当我触摸演员时,它似乎只是跳到随机点。这是我的演员 class、
中的一些代码
public MyActor(){
setBounds(sprite.getX(),sprite.getY(),
sprite.getWidth(),sprite.getHeight());
setTouchable(Touchable.enabled);
addListener(new InputListener(){
@Override
public boolean touchDown(InputEvent event, float x, float y,
int pointer, int button) {
MoveByAction mba = new MoveByAction();
mba.setAmount(x,y);
MyActor.this.addAction(mba);
return true;
}
});
}
@Override
protected void positionChanged() {
sprite.setPosition(getX(),getY());
super.positionChanged();
}
@Override
public void draw(Batch batch, float parentAlpha) {
sprite.draw(batch);
}
@Override
public void act(float delta){
super.act(delta);
}
这里没有什么要问的。首先,您的触摸仅限于触摸您的 Actor。那就是演员所在的地方。您需要在场景级别实现一些基本状态机以了解 "on first tap, this actor is selected",然后它必须处于 "select where this actor goes" 状态,最后当您 select 一个位置时它必须发送那个XY 给 selected 演员做动作。
现在,正如Tenfour04所说,你必须弄清楚触摸点的屏幕XY到游戏世界坐标的转换。 Tenfour04 对在视口相机上使用投影方法来实现这一点做出了很好的评论。一旦你这样做了,你就可以将坐标发送给你的演员来做前面提到的事情。
为了实现移动,我会使用 Action 框架,如下所示:
actor.addAction(移动到(x, y, 0.4f, Interpolation.circle));
此页面向您展示了 Scene2d 可用的所有不错的操作:https://github.com/libgdx/libgdx/wiki/Scene2d#actions
希望这就是您所需要的。 :)
所以我有一个演员,它是一个精灵,设置在屏幕视口舞台上。我想要做的是能够触摸演员,然后触摸屏幕上的一个点,它会流畅地移动。目前,当我触摸演员时,它似乎只是跳到随机点。这是我的演员 class、
中的一些代码public MyActor(){
setBounds(sprite.getX(),sprite.getY(),
sprite.getWidth(),sprite.getHeight());
setTouchable(Touchable.enabled);
addListener(new InputListener(){
@Override
public boolean touchDown(InputEvent event, float x, float y,
int pointer, int button) {
MoveByAction mba = new MoveByAction();
mba.setAmount(x,y);
MyActor.this.addAction(mba);
return true;
}
});
}
@Override
protected void positionChanged() {
sprite.setPosition(getX(),getY());
super.positionChanged();
}
@Override
public void draw(Batch batch, float parentAlpha) {
sprite.draw(batch);
}
@Override
public void act(float delta){
super.act(delta);
}
这里没有什么要问的。首先,您的触摸仅限于触摸您的 Actor。那就是演员所在的地方。您需要在场景级别实现一些基本状态机以了解 "on first tap, this actor is selected",然后它必须处于 "select where this actor goes" 状态,最后当您 select 一个位置时它必须发送那个XY 给 selected 演员做动作。
现在,正如Tenfour04所说,你必须弄清楚触摸点的屏幕XY到游戏世界坐标的转换。 Tenfour04 对在视口相机上使用投影方法来实现这一点做出了很好的评论。一旦你这样做了,你就可以将坐标发送给你的演员来做前面提到的事情。
为了实现移动,我会使用 Action 框架,如下所示: actor.addAction(移动到(x, y, 0.4f, Interpolation.circle));
此页面向您展示了 Scene2d 可用的所有不错的操作:https://github.com/libgdx/libgdx/wiki/Scene2d#actions
希望这就是您所需要的。 :)