从舞台删除所有孩子AS3,舞台变成一个空的对象? [英] AS3 after removing all children from stage, stage becomes a null object?

查看:295
本文介绍了从舞台删除所有孩子AS3,舞台变成一个空的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用这个code在我的AS3文档类从舞台中删除的所有对象:

I have used this code in my AS3 document class to remove all objects from the stage:

var _stage:DisplayObjectContainer = stage as DisplayObjectContainer;
while (_stage.numChildren > 0) {
    _stage.removeChildAt(0);
}

和这似乎是工作非常好但有一个例外。我运行此之后,一个按钮即可pssed重新加载一切搬上舞台$ P $。在这种结构的功能,某些条件语句被添加到创建事件侦听器为舞台,如果他们不存在

and this appears to be working very well with one exception. After I run this, a button can be pressed to re-load everything onto the stage. In this construction function, some conditionals are added to create event listeners for the stage if they don't already exist:

if(!stage.hasEventListener(KeyboardEvent.KEY_DOWN));
    stage.addEventListener(KeyboardEvent.KEY_DOWN, handle_key);
if(!stage.hasEventListener(MouseEvent.MOUSE_MOVE));
    stage.addEventListener(MouseEvent.MOUSE_MOVE, manage_cursor);   

编辑:舞台肯定是空的,我把如果(阶段){} 解决此节code和错误出现了在在C的阶段,$ C $下一个点时

stage definitely is null, I put if(stage){} around this section of code, and the error cropped up at the next point in the code that stage is used

然而,我收到一个错误,在重新构建,类型错误:错误#1009:无法访问空对象引用的属性或方法中提到的舞台 。

I receive an error however, on the reconstruct, TypeError: Error #1009: Cannot access a property or method of a null object reference. in reference to "stage".

进一步的研究表明,有可能是从舞台删除所有的DisplayObject删除访问舞台本身,直到一个DisplayObject在加的能力。然而,这没有任何意义,我无论如何,我不完全知道如何着手。

Further research indicates that it might be possible that removing all DisplayObjects from the stage removes the ability to access the stage itself until a DisplayObject is added in. however, this doesn't make any sense to me whatsoever and I am not entirely sure how to proceed.

任何帮助将是很大的AP preciated。

Any help would be greatly appreciated.

推荐答案

如果您是从一个影片剪辑中调用舞台,引用将只非空当影片剪辑在显示列表上。舞台一旦闪存加载总是存在的,但个人的影片剪辑实例能够获得/损失的参考,当他们被添加/从显示列表中删除。

If you are calling "stage" from within a MovieClip, the reference will ONLY be non-null when the MovieClip is on the display list. The Stage always exists once Flash is loaded, but individual MovieClip instances can gain/lose reference to it when they are added/removed from the display list.

这甚至适用于您的文档根目录的实例。只要任何的DisplayObject从显示列表中删除,其阶段引用设置为null。

This even applies to your document root instance. As soon as any DisplayObject is removed from the display list, its' stage reference is set to null.

下面是使用说明了这个概念根文档的例子:

Here's an example using the root document that illustrates the concept:

package
{
    import flash.display.Sprite;
    import flash.events.Event;

    [SWF(width="800", height="600", frameRate="60"]
    public class Main extends Sprite
    {
        public function Main()
        {
            addEventListener(Event.ENTER_FRAME, onEnterFrame, false, 0, true);
        }

        private function onEnterFrame(event:Event):void
        {
            if (stage != null)
            {
                trace("stage: "+stage);
                stage.removeChild(this);
                trace("stage: "+stage);

               removeEventListener(Event.ENTER_FRAME, onEnterFrame);
            }
        }
    }
}

这code将输出:

stage: [object Stage]
stage: null

请注意舞台参考的损失从显示列表中删除的对象之后。

Note the loss of the stage reference after removing the object from the display list.

在你的榜样,你在舞台上的每一个孩子遍历和删除它们。这肯定会导致因同一概念的阶段,参考丢失,如上图所示。

In your example, you are looping over every child of the stage, and removing each of them. That will definitely cause your stage reference to be lost because of the same concept as shown above.

这篇关于从舞台删除所有孩子AS3,舞台变成一个空的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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