Air for android application 运行一段时间后变慢了,怎么办?

Air for android application runs slow after a while, what to do?

我正在使用 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)

//__________________________ 不同按钮的事件侦听器,发送到每个级别 _____________

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

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

//_________________________For 数组中事物的循环 _________________________

        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;
            }

当然要用 Scout 看看你的内存是如何被使用的。

这里有一个很棒的内存跟踪 class,您可以使用它来检查您的实例是否在应有的时间从内存中释放。

http://divillysausages.com/blog/tracking_memory_leaks_in_as3

所以无论我做什么,我都无法让 Adob​​e scout 工作。但我确实解决了问题。 对谁有帮助: 我在文档 class' 的 ENTER_FRAME 函数中添加了 'trace(System.totalMemory);',这让我能够看到我的应用程序使用了多少内存。开始的时候,数量是不断增加的。 我将所有事件侦听器从主要 class 移动到最合适的 class 中。 例如,我将 'gamePanel.jetButton.addEventListener(MouseEvent.MOUSE_DOWN, jetPackUp)' 移动到游戏面板 class 中。 要从游戏面板 class 中引用主 class,我使用了代码:'MovieClip(parent).someFunction()' 现在我几乎有了一个稳定的应用程序,不会一直使用内存。 当我必须添加同一对象的更多实例时,我只需要弄清楚如何处理我的 for 循环,这会导致内存泄漏。但我认为 'object pooling' 会解决这个问题。