PDA

View Full Version : How to unload external swf?


Thomas_h
11-12-2007, 03:46 PM
Hi,

I'm really new to ActionScript and coding in general, so bear with me.

This is the function I want to do: roll_over loads an external swf. roll_out unloads it. The loading I got figures, but I can't get it unloaded again. Hope you can help.

Here's my code:



button1_mc.addEventListener(MouseEvent.ROLL_OVER, onRollover);
button1_mc.addEventListener(MouseEvent.ROLL_OUT, onRollout);


function onRollover(event:MouseEvent):void
{
var imageRequest:URLRequest = new URLRequest("tekst1.swf");
var imageLoader:Loader = new Loader();
imageLoader.load(imageRequest);
addChild(imageLoader);
imageLoader.y = 340;
imageLoader.x = 25;
}

function onRollover(event:MouseEvent):void
{
WHAT TO DO?
}


Thanks!

tarafenton
11-12-2007, 03:58 PM
removeChild(imageLoader);

ryryguy
11-12-2007, 06:23 PM
Tara definitely has the right idea. However, as coded her solution won't quite work, because "imageLoader" is a variable declared in onRollover, and can't be accessed from onRollout.

The way I would do it is make the loader a class- or timeline-level variable so it can be accessed from both functions. Also, no need to reload the content each time, once should be enough. Like this:

var imageLoader:Loader; // starts out null


function onRollover(event:MouseEvent):void
{
if( imageLoader == null )
{ // first time, create the loader and tell it to load now.
var imageRequest:URLRequest = new URLRequest("tekst1.swf");
imageLoader = new Loader();
imageLoader.load(imageRequest);
}

// the rest happens every time there's a rollover
// (whether the loader was just created or created previously)
addChild(imageLoader);
imageLoader.y = 340;
imageLoader.x = 25;
}

function onRollout(event:MouseEvent):void
{
removeChild(imageLoader);
}

stompwampa
11-13-2007, 03:30 AM
ryryguy's solution will work just fine, but I find this to be easier:

You just declare your variable outside of the functions, then all you do inside of the functions is add/removeChild.
I'm not sure which of our solutions is considered better practice though....for something this simple, I'd just pick whichever you prefer :-)


button1_mc.addEventListener(MouseEvent.ROLL_OVER, onRollover);
button1_mc.addEventListener(MouseEvent.ROLL_OUT, onRollout);

var imageRequest:URLRequest = new URLRequest("tekst1.swf");
var imageLoader:Loader = new Loader();
imageLoader.load(imageRequest);

function onRollover(event:MouseEvent):void
{
addChild(imageLoader);
imageLoader.y = 340;
imageLoader.x = 25;
}

function onRollover(event:MouseEvent):void
{
removeChild(imageLoader);
}

Thomas_h
11-15-2007, 11:19 AM
Tara definitely has the right idea. However, as coded her solution won't quite work, because "imageLoader" is a variable declared in onRollover, and can't be accessed from onRollout.

The way I would do it is make the loader a class- or timeline-level variable so it can be accessed from both functions. Also, no need to reload the content each time, once should be enough. Like this:

var imageLoader:Loader; // starts out null


function onRollover(event:MouseEvent):void
{
if( imageLoader == null )
{ // first time, create the loader and tell it to load now.
var imageRequest:URLRequest = new URLRequest("tekst1.swf");
imageLoader = new Loader();
imageLoader.load(imageRequest);
}

// the rest happens every time there's a rollover
// (whether the loader was just created or created previously)
addChild(imageLoader);
imageLoader.y = 340;
imageLoader.x = 25;
}

function onRollout(event:MouseEvent):void
{
removeChild(imageLoader);
}

Thanks that worked great.

But I'm trying to understand the script completely.


if( imageLoader == null


Can you explain the function of "null"?

Oh, and another ting. Instead of making functions for each button, I'm sure I can make an if-statement. But I'm not quite sure, how to do that.

Thanks :)

stompwampa
11-15-2007, 01:16 PM
null literally means nothing. It's has no data, no numbers, no text...nothing.
Basically, you create the imageLoader and set it equal to null.
It has to have something in it, and by setting it to null, you tell flash that the "something" inside of it is really nothing at all.
It seems kinda of silly, but if you don't do it, flash doesn't know that there
really isn't supposed to be something in there.

I hope that makes sense...

Thomas_h
11-15-2007, 02:01 PM
Okay thanks. By the way, stompwampa, I couldn't get your code to work. It won't let me acces imageLoader with removeChild in that way.

creynders
11-15-2007, 02:04 PM
Just a small correction to stomp's explanation: you don't set it to null, you check whether it equals null. Quite a difference.
assignment operator: '='
equality operator: '=='

ryryguy
11-15-2007, 04:00 PM
Yeah, null is also the default value for an object variable. So in my code checking to see if the loader variable equals null is a way of seeing, "did I create the Loader yet?"

stomp's code which fires up the loader right away is better if you are expecting the loaded resource to be used in most cases. My code, which waits until you're sure you really need it before loading it, might be better if it's not commonly used or maybe is a very large resource.

You can indeed use a single event handling function for multiple buttons with an "if" statement, just check the "target" property of the event argument like this:

function onRollover(event:MouseEvent):void
{
if( event.target == button_mc1 )
{
// do the button_mc1 thing
}
else
if( event.target == button_mc2 )
{
// do the button_mc2 thing
}

}