Flash 从舞台上移除电影唇

Flash 从舞台上移除电影唇,flash,actionscript,stage,Flash,Actionscript,Stage,我正在用FlashActionScript3.0制作一个侧滚游戏。现在我试着让玩家通过触摸某个物体进入下一关。在加载下一个关卡之前,我尝试从舞台上移除上一关卡的所有资产(背景和“地板”),它们是MovieClips。当下一关加载时,我仍然可以看到上一关的资产,尽管玩家不能与它们交互 我环顾了一下互联网,尝试了一些方法来解决我的问题,但我一无所获。下面是我用来从一个级别转换到下一个级别的代码。“bgContainer”仅包含卸载层的背景MovieClip,“allContainer”包含卸载层的地

我正在用FlashActionScript3.0制作一个侧滚游戏。现在我试着让玩家通过触摸某个物体进入下一关。在加载下一个关卡之前,我尝试从舞台上移除上一关卡的所有资产(背景和“地板”),它们是MovieClips。当下一关加载时,我仍然可以看到上一关的资产,尽管玩家不能与它们交互

我环顾了一下互联网,尝试了一些方法来解决我的问题,但我一无所获。下面是我用来从一个级别转换到下一个级别的代码。“bgContainer”仅包含卸载层的背景MovieClip,“allContainer”包含卸载层的地板和其他环境对象。这些容器稍后将加载下一级别所需的对象

// 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  
//检查玩家是否接触到1级的终点
私有函数检查级别1到2集合(事件:事件):无效{
//玩家是否达到了1级的终点?
如果(命中测试相交(玩家,1级2,此)){
//从舞台上移除级别1的背景
//阶段。移除child(bgContainer);
removeEventListener(Event.ENTER_FRAME,scrollScreen);
//从舞台上清除一切(仅当舞台上已经有东西时)
//while(numChildren>0){
//for(var stgIndex=0;stgIndex
可以看出,我已经尝试了至少两种技术来卸载上一级别的资产。我已经尝试过使用“for”循环遍历该阶段并逐个删除所有元素。我已经尝试删除索引0处的stage元素,而stage上有一个对象。在使用addChildAt()添加对象时,我还尝试引用“root”而不是“stage”。这些技术都不起作用。我仍然不知道为什么上一级不会卸载

任何帮助都将不胜感激


谢谢

如果您不确定allContainer的父级是什么,请使用

allContainer.parent && allContainer.parent.removeChild(allContainer.parent);
(左侧仅充当保护,以确保仅当allContainer位于舞台上时才调用右侧,您也可以将其写为:)

您编写的for循环也有问题,因为在删除0处的第一个子项后,子项都向下移动一个索引,因此1处的子项现在为0,但您的索引已移动到1,因此您一直缺少子项!相反,请使用以下命令:

while (allContainer.numChildren)
{
    allContainer.removeChildAt(0);
}
这样,while循环将继续循环,直到删除allContainer的所有子级

或者,如果您想让它以最佳速度运行,请使用

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

希望这有帮助。

如果您不确定allContainer的父级是什么,请使用

allContainer.parent && allContainer.parent.removeChild(allContainer.parent);
(左侧仅充当保护,以确保仅当allContainer位于舞台上时才调用右侧,您也可以将其写为:)

您编写的for循环也有问题,因为在删除0处的第一个子项后,子项都向下移动一个索引,因此1处的子项现在为0,但您的索引已移动到1,因此您一直缺少子项!相反,请使用以下命令:

while (allContainer.numChildren)
{
    allContainer.removeChildAt(0);
}
这样,while循环将继续循环,直到删除allContainer的所有子级

或者,如果您想让它以最佳速度运行,请使用

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

希望这能有所帮助。

您尝试过this.removeChild而不是stage.removeChild吗?您可以发布代码,将元素添加到stage和容器中吗?@AmyBlankenship我尝试使用“this.removeChild”而不是“stage.removeChild”。但这会导致错误,即“this”不是显示对象的子对象。我只是再试了一次,但它没有做任何事情。添加子项时,您没有提供代码,因此我猜测可能是您将子项添加到了“this”,而不是stage。@AmyBlankenship:我使用“addChild()”函数将子项添加到容器(即MovieClips)中。因此,如果我在“envContainer”中添加了一些内容,我会使用以下命令:“envContainer.addChild(object)。”当我的对象被添加到各自的容器中时,我会使用“stage.addChild(container)”将容器添加到stage中。“您是否尝试过这个.removeChild,而不是stage.removeChild?您可以发布代码,将元素添加到stage和您的容器中吗?@AmyBlankenship我尝试使用“this.removeChild”而不是“stage.removeChild”。但这会导致错误,说“this”不是显示对象的子对象。我只是再试了一次,但它没有起任何作用。添加子对象时,您没有提供代码,因此我猜测可能是您将子对象添加到了“this”,而不是stage。@AmyBlankenship:我将子对象添加到容器中(它们是