【问题标题】:Libgdx MoveToAction - Can't get it to workLibgdx MoveToAction - 无法让它工作
【发布时间】:2014-10-21 12:52:52
【问题描述】:

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

    }

【问题讨论】:

    标签: java libgdx scene2d


    【解决方案1】:

    当你创建动作时,就是你设置目标位置的地方,而不是在 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
    }
    

    【讨论】:

      猜你喜欢
      • 2014-05-10
      • 2016-02-24
      • 1970-01-01
      • 2013-09-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-11
      • 1970-01-01
      • 2011-10-18
      相关资源
      最近更新 更多