-
[RESOLVED] Another removeChild problem...
Friends,
This little bit of code is in a click event handler that among many other things is supposed to load and unload an image. Well, it loads the image just fine, but on the second click through the image remains. What am I missing?
Thanks.
Actionscript Code:
switch (clickNumber) { case 0 : var mainImageLoader:Loader = new Loader(); mainImageLoader.load(new URLRequest("large/"+logoArray[firstCard]+".jpg")); addChild(mainImageLoader); mainImageLoader.x = 640; mainImageLoader.y = 65; clickNumber++; break; case 1 : removeChild(mainImageLoader); break; default : trace("ERROR: Not 0 or 1"); }
-
Senior Member
mainImageLoader.parent.removeChild(mainImageLoader );
-
That didn't seem to work. It's as if the mainImageLoader disappears after it's left the switch statement...
Actionscript Code:
switch (clickNumber) { case 0 : var mainImageLoader:Loader = new Loader(); mainImageLoader.load(new URLRequest("large/"+logoArray[firstCard]+".jpg")); addChild(mainImageLoader); mainImageLoader.x = 640; mainImageLoader.y = 65; clickNumber++; trace(mainImageLoader); break; case 1 : trace(mainImageLoader); mainImageLoader.parent.removeChild(mainImageLoader ); break; default : trace("ERROR: Not 0 or 1"); }
Outputs...
[object Loader]
null
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at concentration_fla::MainTimeline/mouseDowned()
-
Senior Member
I cannot determine from the code if a swtich statement would cause this issue can you use if statements instead of switch statment to see if it straightens up?
-
Same output in an IF statement, weird.
-
It's because your mainImageLoader variable is local to the function. After the first execution, that variable falls out of scope. In the second execution, a new variable mainImageLoader is created, which is not the same one as the first execution. Since it is not initialized when clicknumber is not 0, you get a nullpointer error.
The easiest way to fix it is to move the declaration of mainImageLoader outside the function.
-
5TonsOfFlax... still saving my bacon after all these years... thanks.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|