
Copy link to clipboard
Copied
I can't seem to remove / unload the external swf files e.g when the carousel.swf (portfolio) is displayed and I press the about button the about content is overlapping the carousel (portfolio) . How can I remove / unload an external swf file from the main flash file and load a new swf file, while at the same time removing garbage collection from memory?
This is the error message(s) I am receiving: "TypeError: Error #2007: Parameter child must be non-null.
at flash.display::DisplayObjectContainer/removeChild()
at index_fla::MainTimeline/Down3()"
import nl.demonsters.debugger.MonsterDebugger;
var d:MonsterDebugger=new MonsterDebugger(this);
stage.scaleMode=StageScaleMode.NO_SCALE;
stage.align=StageAlign.TOP_LEFT;
stage.addEventListener(Event.RESIZE, resizeHandler);
// loader is the loader for portfolio page swf
var loader:Loader;
var loader2:Loader;
var loader3:Loader;
var loader1:Loader;
// resize content
function resizeHandler(event:Event):void {
// resizes portfolio page to center
loader.x = (stage.stageWidth - loader.width) * .5;
loader.y = (stage.stageHeight - loader.height) * .5;
// resizes about page to center
loader3.x = (stage.stageWidth - 482) * .5 - 260;
loader3.y = (stage.stageHeight - 492) * .5 - 140;
/*loader2.x = (stage.stageWidth - 658.65) * .5;
loader2.y = (stage.stageHeight - 551.45) * .5;*/
}
addEventListener(Event.ENTER_FRAME, onEnterFrame,false, 0, true);
function onEnterFrame(ev:Event):void {
var requesterb:URLRequest=new URLRequest("carouselLoader.swf");
loader = null;
loader = new Loader();
loader.name ="carousel1"
//adds gallery.swf to stage at begining of movie
loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioError);
function ioError(event:IOErrorEvent):void {
trace(event);
}
try {
loader.load(requesterb);
} catch (error:SecurityError) {
trace(error);
}
addChild(loader);
loader.x = (stage.stageWidth - 739) * .5;
loader.y = (stage.stageHeight - 500) * .5;
// stop gallery.swf from duplication over and over again on enter frame
removeEventListener(Event.ENTER_FRAME, onEnterFrame);
}
//PORTFOLIO BUTTON
//adds eventlistner so that gallery.swf can be loaded
MovieClip(root).nav.portfolio.addEventListener(MouseEvent.MOUSE_DOWN, Down, false, 0, true);
function Down(event:MouseEvent):void {
// re adds listener for contact.swf and about.swf
MovieClip(root).nav.info.addEventListener(MouseEvent.MOUSE_DOWN, Down1, false, 0, true);
MovieClip(root).nav.about.addEventListener(MouseEvent.MOUSE_DOWN, Down3, false, 0, true);
//unloads gallery.swf from enter frame if users presses portfolio button in nav
var requester:URLRequest=new URLRequest("carouselLoader.swf");
loader = null;
loader = new Loader();
loader.name ="carousel"
loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioError);
function ioError(event:IOErrorEvent):void {
trace(event);
}
try {
loader.load(requester);
} catch (error:SecurityError) {
trace(error);
}
addChild(loader);
loader.x = (stage.stageWidth - 739) * .5;
loader.y = (stage.stageHeight - 500) * .5;
removeChild( getChildByName("about") );
removeChild( getChildByName("carousel1") );
// remove eventlistner and prevents duplication of gallery.swf
MovieClip(root).nav.portfolio.removeEventListener(MouseEvent.MOUSE_DOWN, Down);
}
//INFORMATION BUTTON
//adds eventlistner so that info.swf can be loaded
MovieClip(root).nav.info.addEventListener(MouseEvent.MOUSE_DOWN, Down1, false, 0, true);
function Down1(event:MouseEvent):void {
//this re-adds the EventListener for portfolio so that end user can view again if they wish.
MovieClip(root).nav.portfolio.addEventListener(MouseEvent.MOUSE_DOWN, Down, false, 0, true);
MovieClip(root).nav.about.addEventListener(MouseEvent.MOUSE_DOWN, Down3, false, 0, true);
var requester:URLRequest=new URLRequest("contactLoader.swf");
loader2 = null;
loader2 = new Loader();
loader2.name ="contact"
loader2.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioError);
function ioError(event:IOErrorEvent):void {
trace(event);
}
try {
loader2.load(requester);
} catch (error:SecurityError) {
trace(error);
}
addChild(loader2);
loader2.x = (stage.stageWidth - 658.65) * .5;
loader2.y = (stage.stageHeight - 551.45) * .5;
// remove eventlistner and prevents duplication of info.swf
MovieClip(root).nav.info.removeEventListener(MouseEvent.MOUSE_DOWN, Down1);
}
//ABOUT BUTTON
//adds eventlistner so that info.swf can be loaded
MovieClip(root).nav.about.addEventListener(MouseEvent.MOUSE_DOWN, Down3, false, 0, true);
function Down3(event:MouseEvent):void {
//this re-adds the EventListener for portfolio so that end user can view again if they wish.
MovieClip(root).nav.portfolio.addEventListener(MouseEvent.MOUSE_DOWN, Down, false, 0, true);
MovieClip(root).nav.info.addEventListener(MouseEvent.MOUSE_DOWN, Down1, false, 0, true);
var requester:URLRequest=new URLRequest("aboutLoader.swf");
loader3 = null;
loader3 = new Loader();
loader3.name ="about"
loader3.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioError);
function ioError(event:IOErrorEvent):void {
trace(event);
}
try {
loader3.load(requester);
} catch (error:SecurityError) {
trace(error);
}
addChild(loader3);
loader3.x = (stage.stageWidth - 482) * .5 - 260;
loader3.y = (stage.stageHeight - 492) * .5 - 140;
removeChild( getChildByName("carousel") );
removeChild( getChildByName("carousel1") );
// remove eventlistner and prevents duplication of info.swf
MovieClip(root).nav.about.removeEventListener(MouseEvent.MOUSE_DOWN, Down3);
}
stop();
1 Correct answer
You call unload() on objects that you nullified - hence the error.
You should do something like:
if(loader2){
loader2.unload();
}
As for the null passed into the function - null is a default value. This way you can call Down pass nothing:
Down()
Or you can use it as an event handler in which case it will be called by listener:
Down(mouseEventInstnace);
Copy link to clipboard
Copied
Your code has several major flaws. You need to clean it up. Below is the first step. It still will not work but there are comments that flag the most obvious things.
Also, I removed some lines for now (like adding an removing mouse listeners on the buttons) to make code more readable - they are not the major concern at this point.
Let me know when you take a look an understand.
Also, when you post your code please tidy it up with indents so people who want to help you have easier time to read it.
import nl.demonsters.debugger.MonsterDebugger;
var d:MonsterDebugger = new MonsterDebugger(this);
stage.scaleMode=StageScaleMode.NO_SCALE.
stage.align=StageAlign.TOP_LEFT;
stage.addEventListener(Event.RESIZE, resizeHandler);
// loader is the loader for portfolio page swf
var loader:Loader;
var loader2:Loader;
var loader3:Loader;
var loader1:Loader;
// resize content
function resizeHandler(event:Event):void {
// resizes portfolio page to center
loader.x = (stage.stageWidth - loader.width) * .5;
loader.y = (stage.stageHeight - loader.height) * .5;
// resizes about page to center
loader3.x = (stage.stageWidth - 482) * .5 - 260;
loader3.y = (stage.stageHeight - 492) * .5 - 140;
}
// you don't need an additional function that called on enter frame to instantiate loader
// you can re-use Down() function
Down();
//PORTFOLIO BUTTON
//adds eventlistner so that gallery.swf can be loaded
MovieClip(root).nav.portfolio.addEventListener(MouseEvent.MOUSE_DOWN, Down, false, 0, true);
// note that that event default is set to null
// this way you can call this function without event
function Down(event:MouseEvent = null):void {
//unloads gallery.swf from enter frame if users presses portfolio button in nav
var requester:URLRequest = new URLRequest("carouselLoader.swf");
loader = null;
loader = new Loader();
loader.name ="carousel"
loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioError);
try {
loader.load(requester);
} catch (error:SecurityError) {
trace(error);
}
addChild(loader);
loader.x = (stage.stageWidth - 739) * .5;
loader.y = (stage.stageHeight - 500) * .5;
// the following two lines will fail if about and corousel1 are not on display list
removeChild( getChildByName("about") );
// you just added carousel1 - why do you remove it?
removeChild( getChildByName("carousel1") );
}
//INFORMATION BUTTON
//adds eventlistner so that info.swf can be loaded
MovieClip(root).nav.info.addEventListener(MouseEvent.MOUSE_DOWN, Down1, false, 0, true);
function Down1(event:MouseEvent):void {
var requester:URLRequest = new URLRequest("contactLoader.swf");
loader2 = null;
loader2 = new Loader();
loader2.name ="contact"
loader2.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioError);
try {
loader2.load(requester);
} catch (error:SecurityError) {
trace(error);
}
addChild(loader2);
loader2.x = (stage.stageWidth - 658.65) * .5;
loader2.y = (stage.stageHeight - 551.45) * .5;
}
//ABOUT BUTTON
//adds eventlistner so that info.swf can be loaded
MovieClip(root).nav.about.addEventListener(MouseEvent.MOUSE_DOWN, Down3, false, 0, true);
function Down3(event:MouseEvent):void {
var requester:URLRequest = new URLRequest("aboutLoader.swf");
loader3 = null;
loader3 = new Loader();
loader3.name ="about"
loader3.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioError);
try {
loader3.load(requester);
} catch (error:SecurityError) {
trace(error);
}
addChild(loader3);
loader3.x = (stage.stageWidth - 482) * .5 - 260;
loader3.y = (stage.stageHeight - 492) * .5 - 140;
// again, the following lines will fail if the objects are not on display list
removeChild( getChildByName("carousel") );
removeChild( getChildByName("carousel1") );
}
// this function doesn't have to be nested inside every mouse event handler
// besides, nested function are bad thing to do
function ioError(event:IOErrorEvent):void {
trace(event);
EventDispatcher(event.target).removeEventListener(IOErrorEvent.IO_ERROR, ioError);
}
stop();

Copy link to clipboard
Copied
Andrei1,
Thank you for the helpful advice. I made the changes as you suggested but I am receiving a #1009 error message even though my site is working the way I wan it to work. I would still like to fix the errors so that my site runs and error free. This is the error I am receiving:
"TypeError: Error #1009: Cannot access a property or method of a null object reference."
I'm sure this is not the best method to unload loaders and I am guessing this is why I am receiving the following error message.
loader.unload();
loader2.unload();
loader3.unload();
I also tried creating a function to unload the loader but received the same error message and my portfolio swf was not showing at all.
function killLoad():void{
try { loader.close(); loader2.close; loader3.close;} catch (e:*) {}
loader.unload(); loader2.unload(); loader3.unload();
}
I have a question regarding suggestion you made to set Mouse Event to "null". What does this do setting the MouseEvent do exactly? Also, since I've set the MouseEvent to null do I also have to set the loader to null? e.g.
---- Here is my updated code ----
// variable for external loaders
var loader:Loader;
var loader1:Loader;
var loader2:Loader;
var loader3:Loader;
// makes borders resize with browser size
function resizeHandler(event:Event):void {
// resizes portfolio page to center
loader.x = (stage.stageWidth - loader.width) * .5;
loader.y = (stage.stageHeight - loader.height) * .5;
// resizes about page to center
loader3.x = (stage.stageWidth - 482) * .5 - 260;
loader3.y = (stage.stageHeight - 492) * .5 - 140;
}
//adds gallery.swf to stage at begining of moviie
Down();
//PORTFOLIO BUTTON
//adds eventlistner so that gallery.swf can be loaded
MovieClip(root).nav.portfolio.addEventListener(MouseEvent.MOUSE_DOWN, Down, false, 0, true);
function Down(event:MouseEvent = null):void {
// re adds listener for contact.swf and about.swf
MovieClip(root).nav.info.addEventListener(MouseEvent.MOUSE_DOWN, Down1, false, 0, true);
MovieClip(root).nav.about.addEventListener(MouseEvent.MOUSE_DOWN, Down3, false, 0, true);
//unloads gallery.swf from enter frame if users presses portfolio button in nav
var requester:URLRequest=new URLRequest("carouselLoader.swf");
loader = new Loader();
loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioError);
function ioError(event:IOErrorEvent):void {
trace(event);
}
try {
loader.load(requester);
} catch (error:SecurityError) {
trace(error);
}
this.addChild(loader);
loader.x = (stage.stageWidth - 739) * .5;
loader.y = (stage.stageHeight - 500) * .5;
// sure this is not the best way to do this - but it is unload external swfs
loader.unload();
loader2.unload();
loader3.unload();
// remove eventlistner and prevents duplication of gallery.swf
MovieClip(root).nav.portfolio.removeEventListener(MouseEvent.MOUSE_DOWN, Down);
}
//INFORMATION BUTTON
//adds eventlistner so that info.swf can be loaded
MovieClip(root).nav.info.addEventListener(MouseEvent.MOUSE_DOWN, Down1, false, 0, true);
function Down1(event:MouseEvent = null):void {
//this re-adds the EventListener for portfolio so that end user can view again if they wish.
MovieClip(root).nav.portfolio.addEventListener(MouseEvent.MOUSE_DOWN, Down, false, 0, true);
MovieClip(root).nav.about.addEventListener(MouseEvent.MOUSE_DOWN, Down3, false, 0, true);
var requester:URLRequest=new URLRequest("contactLoader.swf");
loader2 = null;
loader2 = new Loader();
loader2.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioError);
function ioError(event:IOErrorEvent):void {
trace(event);
}
try {
loader2.load(requester);
} catch (error:SecurityError) {
trace(error);
}
addChild(loader2);
loader2.x = (stage.stageWidth - 658.65) * .5;
loader2.y = (stage.stageHeight - 551.45) * .5;
loader.unload();
loader2.unload();
loader3.unload();
// remove eventlistner and prevents duplication of info.swf
MovieClip(root).nav.info.removeEventListener(MouseEvent.MOUSE_DOWN, Down1);
}
//ABOUT BUTTON
//adds eventlistner so that info.swf can be loaded
MovieClip(root).nav.about.addEventListener(MouseEvent.MOUSE_DOWN, Down3, false, 0, true);
function Down3(event:MouseEvent = null):void {
//this re-adds the EventListener for portfolio so that end user can view again if they wish.
MovieClip(root).nav.portfolio.addEventListener(MouseEvent.MOUSE_DOWN, Down, false, 0, true);
MovieClip(root).nav.info.addEventListener(MouseEvent.MOUSE_DOWN, Down1, false, 0, true);
var requester:URLRequest=new URLRequest("aboutLoader.swf");
loader3 = null;
loader3 = new Loader();
loader3.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioError);
function ioError(event:IOErrorEvent):void {
trace(event);
}
try {
loader3.load(requester);
} catch (error:SecurityError) {
trace(error);
}
addChild(loader3);
loader3.x = (stage.stageWidth - 482) * .5 - 260;
loader3.y = (stage.stageHeight - 492) * .5 - 140;
loader.unload();
loader2.unload();
loader3.unload();
// remove eventlistner and prevents duplication of info.swf
MovieClip(root).nav.about.removeEventListener(MouseEvent.MOUSE_DOWN, Down3);
}
stop();
"
Copy link to clipboard
Copied
You call unload() on objects that you nullified - hence the error.
You should do something like:
if(loader2){
loader2.unload();
}
As for the null passed into the function - null is a default value. This way you can call Down pass nothing:
Down()
Or you can use it as an event handler in which case it will be called by listener:
Down(mouseEventInstnace);

Copy link to clipboard
Copied
Andrei1! Thank for ALL of you help! It works great... One additional question. How would I make my load swf(s) scale in size similar to http://www.forbiddenkingdommovie.com/main.html ?
- Jerrell
Copy link to clipboard
Copied
You are welcome.
I don't understand you last question. How do you want to scale it? What is is in the link you provided that scales the way you want it?

Copy link to clipboard
Copied
The link is http://www.forbiddenkingdommovie.com/main.html . If you selected Normal as the option to access the website. If minimize your browser in size the image will decrease in size however if you maximize your screen the image will increase in size.
My guess is a for loop is necessary to increment on the scale ... but I don't know how i would begin to structure it.
Copy link to clipboard
Copied
I don't observe resizing. It looks like they just change position. event if they do resize it a bit - they do it on resize. You already has the function that resizes repositions the objects - you can plug in resizing too. May look ugly though.
What would be a definition of resizing in your case? It is not as straightforward as it looks.

Copy link to clipboard
Copied
Andrei1,
I thought the "if statement" did the trick.. but I was wrong. If I click on a button more than once the .swf is duplicated and simply will not unload..
I did some research and decided to try a "switch statement" rather than an "if statement" because it is executed faster but I am still unable to unload external swf when loaded and I am receiving a 3590 error:
"flash.net:URLRequest used where a Boolean value was expected. The expression will be type coerced to Boolean.

Copy link to clipboard
Copied
Andrei1,
I thought the "if statement" did the trick.. but I was wrong. If I click on a button more than once the .swf is duplicated and simply will not unload..
I did some research and decided to try a "switch statement" rather than an "if statement" because it is executed faster but I am still unable to unload external swf when loaded and I am receiving a 3590 error:
"flash.net:URLRequest used where a Boolean value was expected. The expression will be type coerced to Boolean."
here is the code I am using:
MovieClip(root).nav.portfolio.addEventListener(MouseEvent.MOUSE_DOWN, Down, false, 0, true);
MovieClip(root).nav.info.addEventListener(MouseEvent.MOUSE_DOWN, Down1, false, 0, true);
var requester:URLRequest=new URLRequest("aboutLoader.swf");
loader3 = new Loader();
loader3.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioError);
function ioError(event:IOErrorEvent):void {
trace(event);
}
try {
switch (loader3.load(requester)) {
case "loader unloaded" :
loader.unloadAndStop(requester);
break;
case "loader2 unloaded" :
loader2.unloadAndStop(requester);
}
} catch (error:SecurityError) {
trace(error);
}
addChild(loader3);
loader3.x = (stage.stageWidth - 482) * .5 - 260;
loader3.y = (stage.stageHeight - 492) * .5 - 140;
Copy link to clipboard
Copied
You use switch incorrectly. The syntax for switch is:
switch([variableToTestAgainst]){
case variableToTestAgainstValue1:
break;
case variableToTestAgainstValue2:
break;
// etc...
}
I already asked you but, again, why do you need to unload them? You can reuse them once they loaded.

Copy link to clipboard
Copied
Initially I was using one loader (loader) to load all three external swf. The reason I am now using three loaders (loader, loader2, and loader3) is because the external swfs differ in size so I had to change math to align the loaders in the center of my main flash file.
e.g.
// makes resize
function resizeHandler(event:Event):void {
// resizes portfolio page to center
loader.x = (stage.stageWidth - loader.width) * .5;
loader.y = (stage.stageHeight - loader.height) * .5;
//resizes contact page to center
loader2.x = (stage.stageWidth - 504.45) * .5 - 260;
loader2.y = (stage.stageHeight - 481.55) * .5 - 140;
// resizes about page to center
loader3.x = (stage.stageWidth - 482) * .5 - 260;
loader3.y = (stage.stageHeight - 492) * .5 - 140;
}
Copy link to clipboard
Copied
This doesn't answer my question why do you need to unload them.

Copy link to clipboard
Copied
I needs to unload my loaders because I do not want the swf files to cluster / duplicate in main flash movie and slower performance as well as for aesthetics.
Copy link to clipboard
Copied
You don't have to duplicate them - you can reuse them by adding to display list or removing them from it once they are loaded. I don't think memory is a big concern because you don't have a lot of swfs to load. In other words, you can load them once and then when user clicks corresponding buttons - add related swfs though addChild. When you want to hide them - just use removeChild()

Copy link to clipboard
Copied
I don't understand... The process of what I thought would be simple removing/ unloading "3" external swf files has become a nightmare. I'm considering going back to AS2.

