Pages

Monday 4 November 2013

ActionScript 3.0 Loading multiple SWF files using a Function

The below code snippet loads multiple SWF files using a single function.


import flash.events.Event;
import flash.events.ProgressEvent;
import flash.events.MouseEvent;
import flash.media.SoundMixer;

// Loading swf files script
// define a startLoad function, also specify a url string parameter
function startLoad(url:String)
{
// create a my_Loader object
var my_Loader:Loader = new Loader();
// create a my_url object and specify url as the parameter
var my_url:URLRequest = new URLRequest(url);
// onCompleteHandler event listener
my_Loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onCompleteHandler);
// onProgressHandler event listener
my_Loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgressHandler);
// load the my_url object in my_Loader loader object
my_Loader.load(my_url);
}

// onCompleteHandler event listener function
function onCompleteHandler(loadEvent:Event)
{
// load the current target in the login_load movieclip
login_load.addChild(loadEvent.currentTarget.content);
// move the login_load movieclip on top of all other movie clips
stage.addChild(login_load);
}

// onProgressHandler event listener function
function onProgressHandler(mProgress:ProgressEvent)
{
// variable percent to calculate percent loader
var percent:Number = mProgress.bytesLoaded / mProgress.bytesTotal;
trace(percent);
}

// stopLoad function which stops the captivate sounds
function stopLoad()
{
SoundMixer.stopAll();
}
// End of loading swf files script

// link buttons which loads swf files
// link_A interactivity
link_A.addEventListener(MouseEvent.CLICK, onlinkClk);

function onlinkClk(evt:MouseEvent):void
{
// call the stopLoad function
stopLoad();
startLoad("swf/Create_Subject_front end_WIP.swf");
}

// link_B interactivity
link_B.addEventListener(MouseEvent.CLICK, onlinkClk1);

function onlinkClk1(evt:MouseEvent):void
{
// call the stopLoad function
stopLoad();
startLoad("swf/Create_Classroom_25th oct.swf");
}
// End of link buttons which loads swf files

You can create a close button which would close and unload the current swf file

// close button to close and unload the swf files
closeBtn.addEventListener(MouseEvent.CLICK, onCloseClk);
function onCloseClk(evt:MouseEvent):void
{
// remove the login_load movieclip from the stage
stage.removeChild(login_load);
SoundMixer.stopAll();
}
// end of close button to close and unload the captivate swf files  

No comments:

Post a Comment