Jump to content
GreenSock

Search In
  • More options...
Find results that contain...
Find results in...
Alicee

Load/Unload swf in AS3

Recommended Posts

Dear Users, I hope you could help me about that (I look around the net but I dont get the informations I need.) I have a (AS3) swf it is palced in a html file than  the end of this swf I would like to load a new swf (with preloader) and unload the previous swf( as clear the memory and stop events).

What is the code with I can earn this result?

Thank you forward for your Help!

Link to comment
Share on other sites

Hi and welcome to the GreenSock forums.
 
Our SWFLoader can handle this task quite easily. Have you read the documentation? 
 
It goes into great detail and explains the methods necessary to load(), unload() and dispose() of a swf. 
 
To figure out when the loaded swf has reached the end of the timeline you can use an ENTER_FRAME event to check if the currentFrame is equal to the totalFrames value.
 
I would suggest for your setup you have 3 swfs
 
1: The swf that does the loading, unloading and tracks the progress of the loading. This swf is embedded in the HTML page
2: the first child swf that loads
3: the second child swf that loads
 
Here is a quick sample of the code
 

 

import com.greensock.*;
import com.greensock.loading.*;
import com.greensock.events.LoaderEvent;
import flash.events.Event;


//create SWFLoaders


var swf1:SWFLoader = new SWFLoader("child1.swf",{container:this,y:100,onProgress:progressHandler,onComplete:completeHandler,autoPlay:false});
var swf2:SWFLoader = new SWFLoader("child2.swf",{container:this,y:100,onProgress:progressHandler,onComplete:completeHandler,autoPlay:false});
var currentSWFLoader:SWFLoader = swf1;


//adjust the progress bar;
function progressHandler(e:LoaderEvent):void {
    bar.scaleX = e.target.progress;
    trace(e.target.progress);
}


//tell the loaded swf to play and start tracking frames
function completeHandler(e:LoaderEvent):void {
    //the target of the LoaderEvent is the SWFLoader that fired the event
    //the rawContent is the loaded swf
    e.target.rawContent.play();
    addEventListener(Event.ENTER_FRAME, checkFrame);
}


function checkFrame(e:Event):void {
    //check to see if loaded swf is done playing
    if (currentSWFLoader.rawContent.currentFrame == currentSWFLoader.rawContent.totalFrames) {
        trace("swf done playing");
        removeEventListener(Event.ENTER_FRAME, checkFrame);


        //if the first swf is done playing load the second swf
        if (currentSWFLoader == swf1) {
            currentSWFLoader.dispose(true) // dispose and unload content
            currentSWFLoader = swf2;
            currentSWFLoader.load();
        }
    }
}


bar.scaleX = 0;
currentSWFLoader.load();
 

 

 
Note, the progress bar works, but the files are very small and the load happens very quickly.
 
CS5 files attached
 
The following tutorials contain videos that explain the basic methods, properties and events that LoaderMax uses. Although they focus on ImageLoaders, the same concepts apply to SWFLoaders
 
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×