Android应用程序的Air运行一段时间后运行缓慢,该怎么办?

时间:2015-01-30 16:27:27

标签: android actionscript-3 flash air

我正在使用Flash Professional制作应用。它基本上是一个先进的避免游戏,有一些物体被生成并在角色之后射击等等。当我在我的手机上打开应用程序时,它按预期顺利运行,但过了一会儿,它运行速度明显变慢,不多,但足以令人讨厌,并且会破坏体验。 我一直小心使用removeChild,以便在清除级别等时删除每个对象。

我可以期待内存泄漏,因为一开始没有延迟吗? 做或查看会有什么好主意? 我应该使用垃圾收集,是否可以使用scout或flash builder分析应用程序?任何好的教程? 提前致谢

// _______________________ Differnet事件监听器______________________

        addEventListener(Event.ENTER_FRAME, moveWorld)

        mainMenu.PlayButton.addEventListener(MouseEvent.CLICK, gotoWorldMap)
        mainMenu.closebutton.addEventListener(MouseEvent.CLICK, closeAppdown)
        gamePanel.jetButton.addEventListener(MouseEvent.MOUSE_DOWN, jetPackUp)
        gamePanel.jetButton.addEventListener(MouseEvent.MOUSE_UP, jetPackStop)
        gamePanel.jumpButton.addEventListener(MouseEvent.MOUSE_DOWN, startJumping)
        gamePanel.jumpButton.addEventListener(MouseEvent.MOUSE_UP, stopSwimming)

        gameOverScreen.RestartButton.addEventListener(MouseEvent.CLICK, restartLevel)
        gameOverScreen.backtoMenu.addEventListener(MouseEvent.CLICK, backtoWorldMap)

        nextLevel.nextLevelButton.addEventListener(MouseEvent.CLICK, gotoNextLevel)
        nextLevel.backtoWorldMap.addEventListener(MouseEvent.CLICK, backworldMap)

        pauseScreen.continueButton.addEventListener(MouseEvent.CLICK, continueGame)

        wannaLeave.YesEndGame.addEventListener(MouseEvent.CLICK, endingGame)
        wannaLeave.backToMain.addEventListener(MouseEvent.CLICK, goingMain)

// __________________________不同按钮的Eventlisteners,发送到每个级别_____________

        worldMap.buttonWorld1.addEventListener(MouseEvent.CLICK, gotoWorld1)
        ...etc...
        worldMap.buttonWorld27.addEventListener(MouseEvent.CLICK, gotoWorld27)

        NativeApplication.nativeApplication.addEventListener(KeyboardEvent.KEY_DOWN,checkKeypress);

// _________________________对于数组中的事物的循环_________________________

        for (var l:int = 0; l<14; l++)
        {
            var lavaThing:LavaThing = new LavaThing;
            lavas.push(lavaThing);
        }
        for (var v:int = 0; v<14; v++)
        {
            var upsideVolcano:UpsideVolcano = new UpsideVolcano;
            volcanos.push(upsideVolcano);
        }
        for (var j:int = 0; j<7; j++)
        {
            var jetFuel:JetFuel = new JetFuel;
            fuels.push(jetFuel);
        }
        for (var m:int = 0; m<3; m++)
        {
            var monster:Monster = new Monster;
            monsterArmy.push(monster);
        }
    }

// _________________让世界走向正确的框架,伴随着危险等...... _______

    public function gotoWorld()
    {
        isonWorldMap = false;
        isinGame = true;

        addChild(backGround);
        backGround.x = 0;
        backGround.y = 0;

        addChild(backGroundPlanet);
        backGroundPlanet.x = 0;
        backGroundPlanet.y = 0;

        addChild(world);
        world.x = 0;
        world.y = 0;
        isinWorld = true

        addChild(character);
        character.x = 100;
        character.y = 350;
        character.gotoAndStop(3);
        addChild(gamePanel);

        addChild(fuelBar);
        fuelBar.x = 521.85;
        fuelBar.y = 492.6;

        if (currentLevel<12)
        {
            Different movespeed
        }else if (currentLevel>11)
        {
            Different Movespeed
        }
        if (currentLevel == 1)
        {
        world.worldDangers.gotoAndStop(1);
        world.SafeGround.gotoAndStop(1);
        world.GroundViz.gotoAndStop(1);
        world.PortaltoNew.gotoAndStop(1);
        world.Water.gotoAndStop(1);

        character.scaleY = 1;
        character.scaleX = 1;


        world.GroundViz.addChild(fuels[0])
        fuels[0].x = 307.15;
        fuels[0].y = -1855.75;

        maxStamina = 500;
        stamina = 0;
        }
        ...etc...
        }else if (currentLevel == 12)
        {
        world.worldDangers.gotoAndStop(12);
        world.SafeGround.gotoAndStop(12);
        world.GroundViz.gotoAndStop(12);
        world.PortaltoNew.gotoAndStop(12);
        world.Water.gotoAndStop(12);
        character.scaleY = 0.7;
        character.scaleX = 0.7;
        world.worldDangers.addChild(lavas[0])
        world.worldDangers.addChild(lavas[1])
        world.worldDangers.addChild(lavas[2])
        world.worldDangers.addChild(lavas[3])
        world.worldDangers.addChild(lavas[4])
        world.worldDangers.addChild(lavas[5])

        lavas[0].x = 83.3;
        lavas[0].y = -356.8;
        lavas[0].rotation = -10

        lavas[1].x = 118;
        lavas[1].y = -130.95;

        lavas[2].x = -3373;
        lavas[2].y = -4433;

        lavas[3].x = -3373;
        lavas[3].y = -4773;

        lavas[4].x = -3373;
        lavas[4].y = -5068;

        lavas[5].x = -3373;
        lavas[5].y = -5408;

        world.GroundViz.addChild(fuels[0])
        world.GroundViz.addChild(fuels[1])
        world.GroundViz.addChild(fuels[2])
        world.GroundViz.addChild(fuels[3])
        world.GroundViz.addChild(fuels[4])

        fuels[0].x = 335.65;
        fuels[0].y = 163.55;
        fuels[1].x = -77.65;
        fuels[1].y = -976.45;
        fuels[2].x = -1292.1;
        fuels[2].y = -1686.9;
        fuels[3].x = -2343.05;
        fuels[3].y = -2696.05;
        fuels[4].x = -3034.2;
        fuels[4].y = -3861.75;


        maxStamina = 500;
        stamina = 0;¨
        ...etc...
        }else if (currentLevel == 13)
        {
        world.worldDangers.gotoAndStop(13);
        world.SafeGround.gotoAndStop(13);
        world.GroundViz.gotoAndStop(13);
        world.PortaltoNew.gotoAndStop(13);
        world.Water.gotoAndStop(13);

        character.scaleY = 0.7;
        character.scaleX = 0.7;



        world.worldDangers.addChild(lavas[0])
        world.worldDangers.addChild(lavas[1])
        world.worldDangers.addChild(lavas[2])
        world.worldDangers.addChild(lavas[3])
        world.worldDangers.addChild(lavas[4])
        world.worldDangers.addChild(lavas[5])
        world.worldDangers.addChild(lavas[6])
        world.worldDangers.addChild(lavas[7])


        lavas[0].x = -2535.75;
        lavas[0].y = -2614.5;

        lavas[1].x = -2535.75;
        lavas[1].y = -2809.6;

        lavas[2].x = -2535.75;
        lavas[2].y = -3019.7;

        lavas[3].x = -2535.75;
        lavas[3].y = -3219.8;

        lavas[4].x = -2790.9;
        lavas[4].y = -4770.65;

        lavas[5].x = -2790.9;
        lavas[5].y = -4985;

        lavas[6].x = -2790.9;
        lavas[6].y = -5195.85;

        lavas[7].x = -2790.9;
        lavas[7].y = -5421;

        world.GroundViz.addChild(fuels[0])
        world.GroundViz.addChild(fuels[1])
        world.GroundViz.addChild(fuels[2])
        world.GroundViz.addChild(fuels[3])
        world.GroundViz.addChild(fuels[4])

        fuels[0].x = 335.6;
        fuels[0].y = 66;
        fuels[1].x = -652.2;
        fuels[1].y = -1031.3;
        fuels[2].x = -2051.15;
        fuels[2].y = -2022.45;
        fuels[3].x = -2165.8;
        fuels[3].y = -3174;
        fuels[4].x = -1547.05;
        fuels[4].y = -4138.65;

        maxStamina = 500;
        stamina = 0;
        }
        ...etc...
        else if (currentLevel == 27)
        {
        world.worldDangers.gotoAndStop(27);
        world.SafeGround.gotoAndStop(27);
        world.GroundViz.gotoAndStop(27);
        world.PortaltoNew.gotoAndStop(27);
        world.Water.gotoAndStop(27);

        character.scaleY = 0.7;
        character.scaleX = 0.7;

        world.GroundViz.addChild(monsterArmy[0])
        monsterArmy[0].x = 1029.75;
        monsterArmy[0].y = -7823.5;

        maxStamina = 2500;
        stamina = 2500;
        }
    }

// ___________________角色遇到危险时调用的功能______________

    public function charDies()
    {
        addChild(gameOverScreen);
        world.parent.removeChild(world);
        backGround.parent.removeChild(backGround);
        backGroundPlanet.parent.removeChild(backGroundPlanet);
        character.parent.removeChild(character);
        gamePanel.parent.removeChild(gamePanel);

        isinWorld = false;
        isinWater = false;
        jetIsBeingPressed = false;
        isinGame = false;
        jumping = false;

//____________________ Removes all the monsters, and volcano obstacles etc....

        for (var h = 0; h < lavas.length; h++) {
        if (lavas[h].parent) { //check to see if this item has a parent
        lavas[h].parent.removeChild(lavas[h]); //tell the parent to remove this child
            }
        }

        for (var v = 0; v < volcanos.length; v++) {
        if (volcanos[v].parent) { //check to see if this item has a parent
        volcanos[v].parent.removeChild(volcanos[v]); //tell the parent to remove this child
            }
        }

        for (var j = 0; j < fuels.length; j++) {
        if (fuels[j].parent) { //check to see if this item has a parent
        fuels[j].parent.removeChild(fuels[j]); //tell the parent to remove this child
            }
        }

        for (var m = 0; m < monsterArmy.length; m++) {
        if (monsterArmy[m].parent) { //check to see if this item has a parent
        monsterArmy[m].parent.removeChild(monsterArmy[m]); //tell the parent to remove this child

        System.pauseForGCIfCollectionImminent()
            }
        }

    }

// ________________让玩家能够再次尝试等级_____________________

    function restartLevel (m:MouseEvent):void 
    {           
        gameOverScreen.parent.removeChild(gameOverScreen);
        gotoWorld()
    }

// _________________级别按钮______________________

    function gotoWorld1 (m:MouseEvent):void
    {
        currentLevel = 1;
        worldMap.parent.removeChild(worldMap);
        gotoWorld();
    }
    ...etc...
    function gotoWorld27 (m:MouseEvent):void
    {
        currentLevel = 27;
        worldMap.parent.removeChild(worldMap);
        gotoWorld();
    }

// ___________________________移动世界_____________________________

    function moveWorld (e:Event)
    {
        trace(System.totalMemory);

        if (isinWorld)
        {
            world.x -= worldMoveSpeed;

            backGroundPlanet.x -= 0.3;

            if (backGroundPlanet.x < -300)
            {
                backGroundPlanet.x = -300;
            }

2 个答案:

答案 0 :(得分:0)

当然可以使用Scout来查看你的记忆是如何被使用的。

这是一个很棒的内存跟踪类,您可以使用它来检查您的实例是否应该从内存中释放。

http://divillysausages.com/blog/tracking_memory_leaks_in_as3

答案 1 :(得分:0)

所以无论我做什么,我都无法让Adobe侦察员工作。但我确实解决了这个问题。 对谁有帮助: 我添加了'trace(System.totalMemory);'在一个ENTER_FRAME函数内的文档类中,这使我能够看到我的应用程序使用了多少内存。一开始,金额不断上升。 我将所有eventlisteners从主类中移动到最适合的任何类中。 例如,我将'gamePanel.jetButton.addEventListener(MouseEvent.MOUSE_DOWN,jetPackUp)'移动到了gamePanel类中。 为了从gamePanel类中引用主类,我使用了代码:'MovieClip(parent).someFunction()' 现在我几乎拥有一个不会继续使用内存的稳定应用程序。 我只需要弄清楚如何处理我的for循环,这会导致内存泄漏,当我必须添加同一对象的更多实例时..但我认为'对象池'将解决这个问题。