AS3 custom class is null after added to array
I'm a C # developer trying to learn some AS3, so this is going to be a pretty new question.
I am confused about scoping and GC since I have my own MovieClip extending class (slide) which I instantiate inside the loop and push () into an array, but then the elements 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 link to the slides later in order to switch them as needed - how can I keep a link to my new objects?
Update: . It looks like there are two problems with this. The forEach request was made before the full URLLoader event was fired, 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);
}
}
);
a source to share
Try it. See if he is tracking 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 cannot assure that it will work. Also, I don't have the rest of the code, so you'll have to test it yourself.
a source to share