自定义AS3类添加到Array后为null
我是一名 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的 Slides.forEach 调用是在事件触发之前执行的,因此幻灯片数组没有值。
Your slides.forEach call is executing before the event is being fired so the slides array has no values.
尝试这个。 让我们看看它是否跟踪您的幻灯片。
我在这里编写它,所以我不能向你保证它会起作用。 另外,我没有其余的代码,所以你必须自己测试。
Try this. Let's see if it traces your slides.
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.