My Actionscript 3.0 Stage will not clear

拟墨画扇 提交于 2020-01-15 11:44:08

问题


I am working on a side-scrolling game in Flash Actionscript 3.0. Right now I am trying to allow the Player to advance to the next level by touching a certain object. Before the next level is loaded, I try to remove all of the previous level's assets (background and "floor"), which are MovieClips, from the stage. When the next level loads, I can still see the previous level's assets, although the Player cannot interact with them.

I have looked around the Internet and tried a few methods to fix my problem, but I have gotten nowhere. Following is the code I use to transition from one level to the next. the 'bgContainer' contains only the unloaded level's background MovieClip and the 'allContainer' contains the unloaded level's floor and other environmental objects. These containers are later loaded with the objects required in the next level.

// Check if the Player has touched Level 1's end point
    private function checkLevel1To2Collision(event:Event):void {
        // Has the Player touched Level 1's end point?
        if (HitTest.intersects(player, level1To2, this)) {

                // Remove Level 1's Background from the stage
                //stage.removeChild(bgContainer);

                removeEventListener(Event.ENTER_FRAME, scrollScreen);

                // Clear everything from the stage (only if there is already something on the stage)
                //while (numChildren > 0) {

                    //for (var stgIndex = 0; stgIndex < stage.numChildren; stgIndex++) {
                        //stage.removeChildAt(0);
                    //}
                //}
                //}

                stage.removeChild(allContainer);
                stage.removeChild(bgContainer);

                // Clear out all elements from the 'allContainer' before reloading the current level
                for (var allIndex1:int = 0; allIndex1 < allContainer.numChildren; allIndex1++) {
                    allContainer.removeChildAt(allIndex1);
                    //if (stage.getChildAt(allIndex1)) {
                        //stage.removeChildAt(allIndex1);
                    //}
                }

                // Remove the elements within 'bgContainer' from the stage
                for (var bgIndex1:int = 0; bgIndex1 < bgContainer.numChildren; bgIndex1++) {
                    bgContainer.removeChildAt(bgIndex1);
                    //if (stage.getChildAt(bgIndex1)) {
                        //stage.removeChildAt(bgIndex1);
                    //}
                }


                // Load Level 2
                loadLevel2(event);
        }
    } // End of 'checkLevel1To2Collision' function  

As can be seen, I have tried at least two techniques to the unload the previous level's assets. I have tried going through the stage and removing all elements one by one using a 'for' loop. And I have tried removing the stage element at index 0 while the stage has an object on it. I also tried referring to 'root' instead of 'stage' while adding objects using 'addChildAt().' None of those techniques worked. I still have no idea why the previous level will not be unloaded.

Any help would be appreciated!

Thanks!


回答1:


If you are unsure what the parent of allContainer is, then use

allContainer.parent && allContainer.parent.removeChild(allContainer.parent);

(this left-side just acts as a guard to ensure that the right-side is called only if allContainer is on the stage, you could alternatively write it as:)

if (allContainer.parent)
{
    allContainer.parent.removeChild(allContainer.parent);
}

The for-loop you write is also problematic, because after you've deleted the first child at 0, the children all shift down one index, so the child at 1 is now at 0 but your index has moved to 1, so you keep missing children! Instead use this:

while (allContainer.numChildren)
{
    allContainer.removeChildAt(0);
}

This way the while loop will keep looping around until all the children of allContainer are removed.

Alternatively, if you want this to run optimally quickly, use

var i:int = allContainer.numChildren;
while (i--)
{
    allContainer.removeChildAt(0);
}

Hope this helps.



来源:https://stackoverflow.com/questions/7733887/my-actionscript-3-0-stage-will-not-clear

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!