Libgdx |演员动作会导致某些设备出现口吃/冻结?

时间:2016-09-06 14:12:26

标签: java android libgdx scene2d

我的游戏现在差不多完成了。我遇到的唯一问题是性能问题。我的游戏在我的机器人涡轮增压器1和2以及星系注释3上运行完美。问题是在我的verizon ellispis上,它会在很短的时间内冻结(就像口吃)。它确实以60 fps运行。我有理由相信这是因为演员行为。我的游戏中有8个敌人(就像飞鸟中的管道一样)被称为阻挡者。这些阻止程序具有旋转操作,moveTo操作以及更改之间空间的自定义操作(gapSizeAction)。当阻挡者从游戏中移除时,口吃就消失了。我将链接下面的相关代码。谢谢!

此代码的工作方式是有3个延迟操作。每次动作消失时,都会导致阻挡者随机移动和/或旋转。创建编队。操作运行后,它们会设置一个随机持续时间,然后重新启动。这些计时器在create方法中初始化一次。因此,您需要查看的唯一代码就是操作本身。我觉得没有任何优化,但我可能是错的。谢谢你的时间!我知道它有很多代码,所以请记住每个阻止程序操作的设置都很重要。

 private void createDelayActions() {

    //Moving RunnableAction
    RunnableAction runnableAction_moving = Actions.run(new Runnable() {
        @Override
        public void run() {
            delayAction_moving.setDuration(Tools.generateFloat(Config.minMovementCooldown, Config.maxMovementCooldown));
            delayAction_moving.restart();

            if (!swinging && !circlifying && !moving && !spinning && !initializing) {
                float maxDuration = 0;
                for (Blocker blocker : blockers) {
                    float gap = Tools.generateFloat(Config.minBlockerGap, Config.maxBlockerGap);
                    float y = Tools.generateFloat(Config.minBlockerY, Config.maxBlockerY - gap);
                    float duration = Tools.generateFloat(Config.minBlockerYDuration, Config.maxBlockerYDuration);
                    blocker.addAction(Actions.after(Actions.moveTo(blocker.getX(), y, duration, Config.blockerInterpolation)));
                    blocker.addAction(GapSizeAction.getGapSizeAction(gap, duration));

                    if (duration > maxDuration) {
                        maxDuration = duration;
                    }
                }

                RunnableAction action = Actions.run(new Runnable() {
                    @Override
                    public void run() {
                        moving = false;
                    }
                });
                stage.addAction(Actions.delay(maxDuration, action));
                moving = true;
            }

            System.out.println("runnableAction_moving");
        }
    });
    delayAction_moving = Actions.delay(2, runnableAction_moving);

    //Spinning RunnableAction
    RunnableAction runnableAction_spinning = Actions.run(new Runnable() {
        @Override
        public void run() {

            delayAction_spinning.setDuration(Tools.generateFloat(Config.minSpinCooldown, Config.maxSpinCooldown));
            delayAction_spinning.restart();

            if (!swinging && !circlifying && !moving && !spinning && !initializing) {
                float spins = Tools.generateFloat(Config.minSpinRotations, Config.maxSpinRotations);

                float duration;

                if (spins < 0) { //With player, clockwise
                    duration = Math.abs(spins * 10); /* The more you multiply @spins by, the 'slower' blocker spins */
                    duration += Tools.generateFloat(-(duration / 3), duration / 3);
                } else { //Against player, counter-clockwise
                    duration = Math.abs(spins * 20); /* The more you multiply @spins by, the 'slower' blocker spins */
                    duration += Tools.generateFloat(-(duration / 3), duration / 3);
                }

                for (Blocker blocker : blockers) {
                    blocker.addAction(Actions.rotateBy(360 * spins, duration, Config.blockerInterpolation));
                }

                RunnableAction action = Actions.run(new Runnable() {
                    @Override
                    public void run() {
                        spinning = false;
                    }
                });
                stage.addAction(Actions.delay(duration, action));
                spinning = true;
            }

            System.out.println("runnableAction_spinning");
        }
    });
    delayAction_spinning = Actions.delay(2, runnableAction_spinning);

    //Swinging RunnableAction
    RunnableAction runnableAction_swinging = Actions.run(new Runnable() {
        @Override
        public void run() {
            delayAction_swinging.setDuration(Tools.generateFloat(Config.minSwingCooldown, Config.maxSwingCooldown));
            delayAction_swinging.restart();

            if (!swinging && !circlifying && !moving && !spinning && !initializing) {

                float spins = Tools.generateFloat(Config.minSpinRotations, Config.maxSpinRotations);
                float duration = Math.abs(spins * 3); /* The more you multiply @spins by, the faster blocker spins */
                duration += Tools.generateFloat(-(duration / 3), duration / 3);

                int rotation = Tools.generateInt(1, 359);
                int type = Tools.generateInt(0, 1);

                /*
                There are two types of swings :
                    -All blockers pile ontop of each other (#0)
                    -Blockers pile every 90 degrees, 4 piles (#1)
                    The type generates the numbers 0 or 1
                */
                float y = Tools.generateFloat(Config.minBlockerY, Config.maxBlockerY);
                float gap = Tools.generateFloat(Config.minBlockerGap, Config.maxBlockerGap);

                for (Blocker blocker : blockers) {

                    Action seqAction = Actions.sequence(
                            GapSizeAction.getGapSizeAction(gap, 2f),
                            Actions.moveTo(blocker.getX(), y, 2f, Config.blockerInterpolation),
                            Actions.sequence(Actions.rotateTo(rotation, 2f, Config.blockerInterpolation), Actions.rotateBy(360 * spins, duration, Config.blockerInterpolation))
                    );

                    blocker.addAction(seqAction);

                    if (type == 1) { //Cause it to increment rotation (by 90 so every quarter) if type == 1 (if not, do original swing (all blockers ontop of each other))
                        rotation += 90;

                        while (rotation > 360) {
                            rotation -= 360;
                        }
                    }
                }

                RunnableAction action = Actions.run(new Runnable() {
                    @Override
                    public void run() {
                        swinging = false;
                        circlifyBlockers(4f);
                    }
                });
                stage.addAction(Actions.delay(duration + 6, action));

                swinging = true;
            }
            System.out.println("runnableAction_swinging");
        }
    });
    delayAction_swinging = Actions.delay(2, runnableAction_swinging);

}

private void circlifyBlockers(float duration) {
    if (blockers.size <= 0) {
        return;
    }
    if (circlifying) {
        return;
    }
    float gap = Tools.generateFloat(Config.minBlockerGap, Config.maxBlockerGap);
    float y = Tools.generateFloat(Config.minBlockerY, Config.maxBlockerY - gap);

    float spacing = 360 / blockers.size;
    float counter = spacing;
    for (Blocker blocker : blockers) {
        Action action = blockerHasAction(blocker, Actions.rotateBy(0, 0));
        if (action != null) { //Then blocker already has spinning action, so remove it before adding new spinning action
            blocker.removeAction(action);
        }
        action = blockerHasAction(blocker, Actions.rotateTo(0, 0));
        if (action != null) { //Then blocker already has spinning action, so remove it before adding new spinning action
            blocker.removeAction(action);
        }
        action = blockerHasAction(blocker, GapSizeAction.getGapSizeAction(0, 0));
        if (action != null) { //Then blocker already has spinning action, so remove it before adding new spinning action
            blocker.removeAction(action);
        }
        action = blockerHasAction(blocker, Actions.moveTo(0, 0));
        if (action != null) { //Then blocker already has spinning action, so remove it before adding new spinning action
            blocker.removeAction(action);
        }
        blocker.addAction(Actions.sequence(Actions.parallel(Actions.moveTo(blocker.getX(), y, duration, Config.blockerInterpolation), GapSizeAction.getGapSizeAction(gap, duration)), Actions.rotateTo(counter, duration, Config.blockerInterpolation)));
        counter += spacing;
    }

    RunnableAction action = Actions.run(new Runnable() {
        @Override
        public void run() {
            circlifying = false;
        }
    });
    stage.addAction(Actions.delay(duration + 6, action));

    circlifying = true;

    System.out.println("Circlify");
}

private void circlifyBlockers(float duration, float y, float gap) {
    if (blockers.size <= 0) {
        return;
    }

    float spacing = 360 / blockers.size;
    float counter = spacing;
    for (Blocker blocker : blockers) {
        Action action = blockerHasAction(blocker, Actions.rotateBy(0, 0));
        if (action != null) { //Then blocker already has spinning action, so remove it before adding new spinning action
            return;
        }
        action = blockerHasAction(blocker, Actions.rotateTo(0, 0));
        if (action != null) { //Then blocker already has spinning action, so remove it before adding new spinning action
            return;
        }
        blocker.addAction(Actions.sequence(Actions.parallel(Actions.moveTo(blocker.getX(), y, duration, Config.blockerInterpolation), GapSizeAction.getGapSizeAction(gap, duration)), Actions.rotateTo(counter, duration, Config.blockerInterpolation)));
        counter += spacing;
    }

    System.out.println("Circlify");
}

 private Action blockerHasAction(Blocker blocker, Action hasAction) {
    for (Action action : blocker.getActions()) {
        if (action.getClass().equals(hasAction.getClass())) {
            return action;
        }
    }
    return null;
}

0 个答案:

没有答案