【问题标题】:small lags when starting the next moveTo action开始下一个 moveTo 动作时的小滞后
【发布时间】:2023-03-29 13:55:02
【问题描述】:

[更新] 我稍微改变了顺序,所以我在方法结束时调用了super.act(delta),这似乎有点帮助!但还不确定。

我的地图有一个正方形系统。所以它是一个二维数组,我做了一个动作,这个数字确实从一个正方形移动到下一个正方形。虽然不可能“停止”两个方格之间的数字。

当我的角色移动时,我检查触摸板是否被触摸,如果是,我开始下一步移动。虽然它有时似乎滞后,我不知道为什么!?我真的希望你能找到“滞后”。以下是我如何计算我的Actor 字符的act() 内的下一步动作:

@Override
public void act(float delta) {
    super.act(delta); // so the actions work
    if (moveDone) {
        if (screen.gameHud.pad.isTouched()) {
            // check in which direction is the touchcontroller
            if (screen.gameHud.pad.getKnobPercentX() < 0
                    && Math.abs(screen.gameHud.pad.getKnobPercentY()) < Math
                            .abs(screen.gameHud.pad.getKnobPercentX())) {
                // checkt if the |x|>|y|
                if (checkNextMove(Status.LEFT)) {
                    this.status = Status.LEFT;
                    move(Status.LEFT);
                    this.screen.map.mapArray[(int) (this.mapPos.x)][(int) this.mapPos.y] = Config.EMPTYPOSITION;
                    this.screen.map.mapArray[(int) (this.mapPos.x - 1)][(int) this.mapPos.y] = Config.CHARSTATE;
                    this.mapPos.x--;
                    moveDone = false;
                }
            } else if //.... rest is the same just with other directions
else{ //if touchpad isnt touched!
    setIdle();
}
updateSprite(delta); //just change the textureRegion if its time for that
} //methode end

好的,我相信您需要更多信息。像这样的检查动作:

case LEFT:
        if (this.mapPos.x - 1 >= 0)
            if (this.screen.map.mapArray[(int) (this.mapPos.x - 1)][(int) this.mapPos.y] == Config.EMPTYPOSITION)
                return true;
        break; //same for all other just direction changin

我猜你需要知道的最后一个是move(_)。它确实为我的人物添加了一个 moveTo Action。

public void move(Status direction) {
    switch (direction) {
    case LEFT:
        this.addAction(Actions.sequence(
                Actions.moveTo((getX() - Config.BLOCK_SIZE), getY(), speed),
                moveDoneAction));
        break;

moveDoneAction 是一个简单的RunnableAction,如果移动完成,它会将boolean moveDone 设置为true。

我真的希望你能帮忙。如果您需要更多信息,请在评论中告诉我!

【问题讨论】:

    标签: java android animation libgdx


    【解决方案1】:

    有比 StackOverflow 更好的工具来优化 Android 代码。使用分析器,看看实际发生了什么。具体来说就是traceview profiler:how to use traceview in eclipse for android development?(如果你不使用Eclipse,可以直接通过ADT使用traceview。)

    【讨论】:

    • 感谢您的建议。但我真的不知道如何使用跟踪。 (MethodTracing) 我做了一个跟踪,但我现在只知道移动 Methode 大约是一个周期的整个计算时间的 70%。
    • 我优化了移动方法,所以我不创建动作的新实例,只需更改一个动作并再次添加它。在跟踪中看起来更好。谢谢
    • 是的,在渲染路径中创建对象会导致打嗝和减速。使用 DDMS 内存分配跟踪器跟踪并消除它们。
    • 其实我猜你的小费确实很好地解决了它。谢谢 将其作为正确答案。感谢 selflerningtip,所以我可以自己修复它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-19
    相关资源
    最近更新 更多