从舞台上删除电影剪辑 [英] Removing movieclips from the stage

查看:140
本文介绍了从舞台上删除电影剪辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Flash Actionscript 3.0进行横向滚动游戏.现在,我正在尝试通过触摸某个对象来使玩家前进到一个新的水平.在加载下一个级别之前,我尝试从舞台中删除上一个级别的所有资产(背景和地板"),它们都是MovieClips.加载下一个级别时,尽管播放器无法与之交互,但仍可以看到上一个级别的资产.

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.

我环顾了互联网,并尝试了几种方法来解决我的问题,但是却一无所获.以下是我用来从一个级别过渡到另一个级别的代码. "bgContainer"仅包含卸载的级别的背景MovieClip,而"allContainer"则包含卸载的级别的地板和其他环境对象.这些容器以后会加载下一级所需的对象.

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  

可以看出,我尝试了至少两种技术来卸载上一级的资产.我尝试过整个阶段,并使用"for"循环逐个删除所有元素.并且我尝试在阶段上有一个对象的同时删除索引为0的stage元素.我还尝试在使用"addChildAt()"添加对象时引用"root"而不是"stage".这些技术都不起作用.我仍然不知道为什么以前的级别不会被卸载.

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.

任何帮助将不胜感激!

谢谢!

推荐答案

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

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

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

(此左侧仅用作保护,以确保仅在allContainer在舞台上时才调用右侧,您也可以将其编写为:)

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

您编写的for循环也是有问题的,因为删除第一个孩子0后,所有孩子都向下移动了一个索引,因此,孩子1现在为0,但是您的索引已移至1.这样你就一直想念孩子们!而是使用此:

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

通过这种方式,while循环将一直循环直到删除allContainer的所有子代.

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

希望这会有所帮助.

这篇关于从舞台上删除电影剪辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆