简体   繁体   English

移动演员Accuratley LibGDX Scene2d

[英]Move Actors accuratley LibGDX Scene2d

So i have an actor which is a sprite, set on a screenviewport stage. 所以我有一个演员,是一个精灵,设置在screenviewport舞台上。 What i want to do is be able to touch the actor, then touch a spot on the screen it will move that fluently. 我想做的是能够触摸演员,然后触摸屏幕上的某个点,它将流畅地移动。 Currently when i touch the actor it just jumps seemingly to random spots. 目前,当我触摸演员时,它似乎只是跳到随机点。 Here is some of the code in my actor 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);
}

Few things to ask here. 这里没什么要问的。 First of all, your touch is only on touching your Actor. 首先,您的触摸只是触摸演员。 That's going to be exactly where the actor is. 那将是演员的确切位置。 You need to implement at your Scene level some basic state machine to know "on first tap, this actor is selected", then it has to be in a "select where this actor goes" state, and finally when you select a position it has to send that XY to the selected actor to do the move. 您需要在“场景”级别实现一些基本状态机,才能知道“初次点击,此角色已被选中”,然后它必须处于“选择此角色去向”状态,最后,当您选择一个位置时,将该XY发送给选定的演员进行移动。

Now, as Tenfour04 mentioned, you have to figure out the conversion of the screen's XY of the touch point into the game world's coordinates. 现在,正如Tenfour04所述,您必须弄清楚触摸点在屏幕上的XY到游戏世界坐标的转换。 Tenfour04 made a good comment about using the projection method on the viewport camera to achieve this. Tenfour04对在视口摄像机上使用投影方法来实现此目标做出了很好的评价。 Once you do that, you can send the coordinates to your actor to do the previously mentioned stuff. 完成此操作后,您可以将坐标发送给演员以执行前面提到的工作。

To achieve the movement, I'd use the Action framework, like below: actor.addAction(moveTo(x, y, 0.4f, Interpolation.circle)); 为了实现运动,我将使用Action框架,如下所示:actor.addAction(moveTo(x,y,0.4f,Interpolation.circle));

This page shows you all the nice actions available for Scene2d: https://github.com/libgdx/libgdx/wiki/Scene2d#actions 此页面显示了可用于Scene2d的所有不错的操作: https : //github.com/libgdx/libgdx/wiki/Scene2d#actions

Hope that's what you needed. 希望这就是您所需要的。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM