自定义AS3类添加到Array后为null

发布于 2024-07-19 07:28:14 字数 1533 浏览 6 评论 0原文

我是一名 C# 开发人员,正在尝试学习一些 AS3,所以这将是一个相当新手的问题。

我对作用域和 GC 感到困惑,因为我有一个自定义的 MovieClip 扩展类(Slide),我在循环中创建实例并将其推送()到数组中,但之后当我拉动它们时这些项目为空从集合中出来。

var ldr:URLLoader = new URLLoader();
ldr.load(new URLRequest("presentation.xml"));
ldr.addEventListener(
    Event.COMPLETE,
     function(e:Event):void {
         config = new XML(e.target.data);
         for (var i:Number = 0; i < config.slides.slide.length(); i++)
         {
             var node = config.slides.slide[i];
             var slide:Slide = new Slide();                      
             slides.push(slide);

             addChild(slide); // Works fine
         }
     }
);

slides.forEach(function(e:*, index:int, array:Array):void
    {
        addChild(e); // Causes "Parameter child must be non-null" exception
    }
);

我希望稍后能够引用幻灯片,以便根据需要切换它们 - 如何保留对新对象的引用?

更新:这似乎有两个问题。 forEach 调用是在调用 URLLoader 的完成事件之前进行的,而且 forEach 似乎也没有按预期工作。 这是最终的工作代码:

var ldr:URLLoader = new URLLoader();
ldr.load(new URLRequest("presentation.xml"));
ldr.addEventListener(
    Event.COMPLETE,
    function(e:Event):void {
        config = new XML(e.target.data);
        for (var i:Number = 0; i < config.slides.slide.length(); i++)
        {
            var node = config.slides.slide[i];
            var slide:Slide = new Slide();                      
            slides.push(slide);
        }
        for each (var sl in slides)
        {
            addChild(sl);
        }
    }
);

I'm a C# developer who is trying to learn some AS3, so this is going to be a pretty newbie question.

I'm getting confused with regards to scope and GC, as I have a custom MovieClip-extending class (Slide) which I create instances of within a loop and push() into an Array, but afterwards the items are null when I pull them out of the collection.

var ldr:URLLoader = new URLLoader();
ldr.load(new URLRequest("presentation.xml"));
ldr.addEventListener(
    Event.COMPLETE,
     function(e:Event):void {
         config = new XML(e.target.data);
         for (var i:Number = 0; i < config.slides.slide.length(); i++)
         {
             var node = config.slides.slide[i];
             var slide:Slide = new Slide();                      
             slides.push(slide);

             addChild(slide); // Works fine
         }
     }
);

slides.forEach(function(e:*, index:int, array:Array):void
    {
        addChild(e); // Causes "Parameter child must be non-null" exception
    }
);

I would like to be able to references the slides later in order to toggle them as needed - how can I keep the reference to my new objects?

Update: It appears there were two problems with this. The forEach call was being made before the URLLoader's complete event was called, and also forEach doesn't seem to work as expected. Here is the final working code:

var ldr:URLLoader = new URLLoader();
ldr.load(new URLRequest("presentation.xml"));
ldr.addEventListener(
    Event.COMPLETE,
    function(e:Event):void {
        config = new XML(e.target.data);
        for (var i:Number = 0; i < config.slides.slide.length(); i++)
        {
            var node = config.slides.slide[i];
            var slide:Slide = new Slide();                      
            slides.push(slide);
        }
        for each (var sl in slides)
        {
            addChild(sl);
        }
    }
);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

最丧也最甜 2024-07-26 07:28:14

您的 Slides.forEach 调用是在事件触发之前执行的,因此幻灯片数组没有值。

Your slides.forEach call is executing before the event is being fired so the slides array has no values.

陌路终见情 2024-07-26 07:28:14

尝试这个。 让我们看看它是否跟踪您的幻灯片。

var ldr:URLLoader = new URLLoader();
ldr.addEventListener(Event.COMPLETE, processXML);
ldr.load(new URLRequest("presentation.xml"));

function processXML(e:Event):void {
    config = new XML(e.target.data);
    var slide:Slide;
    for (var i:Number = 0; i < config.slides.slide.length(); i++)
    {
        var node = config.slides.slide[i];
        slide = new Slide();                                          
        slides.push(slide);

        addChild(slide); // Works fine
    }

    for each(var slide:Slide in slides){
        trace(slide);
    }
 }

我在这里编写它,所以我不能向你保证它会起作用。 另外,我没有其余的代码,所以你必须自己测试。

Try this. Let's see if it traces your slides.

var ldr:URLLoader = new URLLoader();
ldr.addEventListener(Event.COMPLETE, processXML);
ldr.load(new URLRequest("presentation.xml"));

function processXML(e:Event):void {
    config = new XML(e.target.data);
    var slide:Slide;
    for (var i:Number = 0; i < config.slides.slide.length(); i++)
    {
        var node = config.slides.slide[i];
        slide = new Slide();                                          
        slides.push(slide);

        addChild(slide); // Works fine
    }

    for each(var slide:Slide in slides){
        trace(slide);
    }
 }

I am coding it here so I can't assure you it'll work. Also, I don't have the rest of the code so you'll have to test it by yourself.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文