gavin sim logo
 

Week 1

Week 2

Week 3

Week 4

Week 5

Week 6

Week 7

Week 8

Week 9

Teaching > ActionScript

Loading SWF

Part 1

Create a new flash document, within this create an animation of a ball moving across the screen. Once complete save this as ball.fla and then publish it as a swf. File / Publish.

 

Part 2

Now create a new flash document, on the stage create a button called loadButton and give it the instance name of loadBT.

Create a new layer and name this actions. The code we will create is very much like the laoding image code but this time we will load in the ball.swf. Type in the following code:

var swfRequest:URLRequest = new URLRequest("monster.swf");
var swfLoader:Loader = new Loader();

loadBT.addEventListener(MouseEvent.CLICK, loadFile);

function loadFile(evt:MouseEvent):void{
     swfLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, swfLoaded, false, 0, true);
     swfLoader.load(swfRequest);
}

function swfLoaded(event:Event):void{
     swfLoader.x=0;
     addChild(swfLoader);
}

The URLRequest variable represent the location of the external file that will be loaded. The loader is a method that enables the file to be loaded. The swfLoader.load() is a method that is instructing the loader to load the swfRequest file in this instance ball.swf. The event listener is listening for the swf to have completly loaded and then it will be added to the stage at 0. You then use the addChild to add the item to the display list which is the stage.

Save the file as loading.fla ensure that it is in the same folder as ball.swf.

 

Part 3

Create a new button called unLoadButton, and give this an instance name of unloadBT.

Now open the actions panel and add the following code:

unloadBT.addEventListener(MouseEvent.CLICK, unloadFile);

function unloadFile(evt:MouseEvent):void{
 removeChild(swfLoader);
}

Once the file has been loaded this will remove the swf from the stage when the button is pressed. This is achieved by using the remove child method.

 

Excercise

If you press the button when the movie has not been loaded this will cause a error in flash as you are trying to remove an object from the stage that is not there. Add additional code to prevent this from happening, you will need to probably use a conditionals but there are lots of ways this can be achieved.