Java Libgdx MoveToAction-无法使其工作

Java Libgdx MoveToAction-无法使其工作,java,libgdx,scene2d,Java,Libgdx,Scene2d,我正在尝试使用MoveToAction更新屏幕上的演员。然而,它似乎没有起到任何作用,我也找不到很多在线示例来帮助我找到的那些建议我正确设置了它,尽管我显然没有。我通过setPositionX方法更新positionX,并且能够通过日志确定positionX正在更新。我还有什么需要补充的吗 public MyActor(boolean playerIsEast, int positionX) { setBounds(this.getX(), 140, 50, 200);

我正在尝试使用MoveToAction更新屏幕上的演员。然而,它似乎没有起到任何作用,我也找不到很多在线示例来帮助我找到的那些建议我正确设置了它,尽管我显然没有。我通过setPositionX方法更新positionX,并且能够通过日志确定positionX正在更新。我还有什么需要补充的吗

public MyActor(boolean playerIsEast, int positionX) {
        setBounds(this.getX(), 140, 50, 200);
        this.positionX = positionX;
        this.setX(400);
        currentImage = AssetLoader.losEast;
        moveAction = new MoveToAction();
        moveAction.setDuration(1);
        this.addAction(moveAction);
    }

    @Override
    public void draw(Batch batch, float alpha) {
        batch.draw(currentImage, this.getX(), 140, 50, 200);
    }

    @Override
    public void act(float delta) {
        moveAction.setPosition(positionX, 140);
        for (Iterator<Action> iter = this.getActions().iterator(); iter
                .hasNext();) {
            iter.next().act(delta);
        }

    }

创建动作时,即设置目标位置的位置,而不是在act方法中。不需要您的act方法来处理动作,因为它已经内置到Actor超类中

所以它应该是这样的:

public MyActor(boolean playerIsEast, int positionX) {
    setBounds(this.getX(), 140, 50, 200);
    this.positionX = positionX;
    this.setX(400);
    currentImage = AssetLoader.losEast;
    moveAction = new MoveToAction();
    moveAction.setDuration(1);
    moveAction.setPosition(positionX, 140);
    this.addAction(moveAction);
}

@Override
public void draw(Batch batch, float alpha) {
    batch.draw(currentImage, this.getX(), 140, 50, 200);
}

@Override
public void act(float delta) {
    super.act(delta); //This handles actions for you and removes them when they finish

    //You could do stuff other than handle actions here, such as 
    //changing a sprite for an animation.
}
但是,最好使用池操作来避免触发GC。Actions类为此提供了方便的方法。因此,您可以进一步简化:

public MyActor(boolean playerIsEast, int positionX) {
    setBounds(this.getX(), 140, 50, 200);
    this.positionX = positionX;
    this.setX(400);
    currentImage = AssetLoader.losEast;
    this.addAction(Actions.moveTo(positionX, 140, 1)); //last argument is duration
}