我的ActionScript 3.0阶段将不明确 [英] My Actionscript 3.0 Stage will not clear

查看:113
本文介绍了我的ActionScript 3.0阶段将不明确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个横向卷轴游戏在Flash ActionScript 3.0中。现在我想允许玩家通过触摸某个对象推进到一个新的水平。在一个新的水平被加载时,我尝试删除所有的previous级的资产(背景和地板),这是影片剪辑中,从舞台。当一个新的水平荷载,我仍然可以看到previous级的资产,虽然球员无法与他们进行互动。

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.

我环顾四周互联网和尝试了一些方法来解决我的问题,但我得到无处。以下是code我用它来转变从一个层次到下一个。在bgContainer只包含卸载水平的背景影片剪辑和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  

如可以看到的,我试图至少两种技术来卸载所述previous水平的资产。我曾尝试去通过阶段,并使用'for'循环由一个删除所有的元素之一。我试图在索引0删除舞台元素,而在舞台上有一个对象。我也试过指的不是阶段到根,同时增加使用对象的addChildAt()。没有这些技术的工作。我仍然不知道为什么previous水平不会被卸载。

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.

任何帮助将是AP preciated!

Any help would be appreciated!

谢谢!

推荐答案

如果您不确定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);
}

希望这有助于。

Hope this helps.

这篇关于我的ActionScript 3.0阶段将不明确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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