// THE LOADER EMBEDS AN EXTERNAL SWF FILE
// AND THE USER NAVIGATES ITS SCENES USING BUTTONS
// IN THE PARENT MOVIE
////////////////////////////////////////////////////
// USES ALL THE LIBRARIES TO DISPLAY
// THE LOADERS
import flash.display.*;
// ALLOWS FLASH TO UNDERSTAND IT WILL USE AN EXTERNAL FILE
// IT CAN BE LOCAL OR HTTP BASED
import flash.net.URLRequest;
import flash.events.Event;
import flash.events.ProgressEvent;
// CREATE THE MASK FOR THE LOADER
// THIS FIRST STEP DRAWS JUST A
// BASIC BLACK RECTANGLE ON THE STAGE
var myMask:Shape = new Shape();
myMask.graphics.beginFill(0x000000);
// X, Y, WIDTH, HEIGHT => SHOULD BE THE SAME
// DIMENSIONS AS YOUR IMPORTED SWF MOVIE
myMask.graphics.drawRect(20, 10, 550, 400); // OUR MOVIE IS 550 X 400 px same coordinates as ldr
myMask.graphics.endFill();
addChild(myMask); // ===> VERY IMPORTANT = CREATES THE RECTANGLE DYNAMICALLY ON THE STAGE
// CREATE THE VARIABLE THAT WILL CONTAIN THE LOADER
var ldr:Loader = new Loader();
// AND NOW TURN THE RECTANGLE YOU CREATED
// INTO A MASK AND ATTACH IT TO THE LOADER
// USING THIS CODE
ldr.mask = myMask;
// CREATE THE VARIABLE THAT WILL CONTAIN THE URL
// OR THE ADDRESS OF THE FILE YOU WILL LOAD INTO
// THE LOADER - THIS FILE CAN BE
// PNG
// NON PROGRESSIVE JPG <==== !!!!
// SWF FILE
var url:String = "SWFwithScenes.swf";
// URLRequest allows FLASH to access the remote file
var urlReq:URLRequest = new URLRequest(url);
// LOAD THE EXTERNAL FILE INTO THE LOADER CONTAINER
ldr.load(urlReq);
// CREATE THE PRELOADER
ldr.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, checkProgress);
// VERIFY IT HAS BEEN LOADED CORRECTLY
ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, ifLoaded);
// ADD ALL THE ACTIONS TO THE BUTTONS IN THIS FUNCTION
ldr.addEventListener(Event.ADDED, ifAdded);
// HERE ARE ALL THE FUNCTIONS
// THIS ONE SHOWS THE PROGRESS OF THE DOWNLOADED FILE
// INTO THE 'PRELOADER' DYNAMIC TEXT FIELD ON THE STAGE
function checkProgress(event:ProgressEvent):void {
// DIVIDE THE BYTES THAT ARE BEING LOADED BY THE
// TOTAL AMOUNT OF BYTES IN YOUR EXTERNAL FILE - A CONSTANT NUMBER
// MULTIPLY BY 100 AND YOU GET A PERCENTAGE - I AM ROUNDING THAT
// NUMBER TO AVOID ANNOYING FLOAT DECIMALS
// YOU CAN USE THAT NUMBER TO MAKE THE WIDTH OF A MOVIE CLIP GROW
// OR CHANGE THE ROTATION OR THE OPACITY OR ANYTHING
// BUT BE CAREFUL BECAUSE THE PURPOSE OF A PRELOADER IS TO HAVE A SMALL FILE
// NOT FANCY GRAPHICS GOING ON SO THE BEST SOLUTION IS TEXT ONLY
//trace(Math.round(event.bytesLoaded/event.bytesTotal*100));
// WITHOUT PERCENT SIGN
//PRELOADER.text = Math.round(event.bytesLoaded/event.bytesTotal*100) + "%";
PRELOADER.text = String(Math.round(event.bytesLoaded/event.bytesTotal*100));
}
// THIS FUNCTION ADDS THE ldr TO THE STAGE ONCE IT HAS BEEN LOADED
// AND HIDES THE PRELOADER TEXT FIELD IN CASE YOU HAVE ANY TRANSPARENCIES
// IN YOUR MOVIE CLIP IF YOU PLACED THE TEXT FIELF BEHIND THE LOADER
function ifLoaded(event:Event):void {
// IF YOU WANT, CHANGE SCALE OF THE IMAGE
// it's a number between 0 and 1
// NOW BE VERY CAREFUL WITH THIS:
// ANY OBJECTS OUTSIDE THE STAGE IN THE IMPORTED MOVIE WILL SHOW UP
// THAT IS WHY I SUGGEST NOT TO SCALE BUT USE AT ITS ORIGINAL SIZE
// THIS IS THE REASON WHY YOU WILL MASK THE LOADER TO AVOID ANY
// UNWANTED ELEMENTS OUTSIDE OF IT.
ldr.scaleX = ldr.scaleY = 1; // 550 X 400 px in our example
// SET THE LOADER'S X AND Y COORDINATES
ldr.x = 20;
ldr.y = 10;
// ADD THE LOADER TO THE STAGE DYNAMICALLY
//addChild(ldr);// <<<< VERY IMPORTANT!!!!!!!!!!
addChildAt(ldr,1); // <<<< USE THIS IF YOU HAVE A BACKGROUND MOVIECLIP
// FOR A TRANSPARENT SWF THAT YOU PLAY IN THE LOADER
// HIDE THE PRELOADER TEXT FIELD
PRELOADER.visible = false;
}
// THIS FUNCTION CONTROLS THE NEW ADDED MOVIE CLIP
// THE BUTTONS ALLOW YOU TO NAVIGATE INSIDE THIS ADDED FILE
// BUT BEFORE YOU HAVE TO CREATE A NEW MOVIE CLIP
// REMEMBER LOADERS HAVE ONLY ONE FRAME SO YOU NEED TO USE THIS EXTRA STEP
// IN ORDER TO BE ABLE TO CONTROL THE EXTERNAL MOVIE CLIP
function ifAdded(event:Event):void {
// CREATE YOUR NEW MOVIE CLIP CONTAINER
var MC:MovieClip = new MovieClip();
// DECLARE THAT MC WILL BE A COPY OF WHATEVER CONTENT
// YOUR LOADER HAS
MC = MovieClip(ldr.content);
// MENU IS A MOVIE CLIP PLACED ON THE STAGE
// THAT CONTAINS A SERIES OF BUTTONS WHOSE INSTANCE NAMES ARE
// INTRO, Scene1, Scene2, Scene3, Scene4, Scene5 and CREDITS
// THESE ARE ALSO THE NAMES OF THE EACH ONE OF THE DISTINCT
// SCENES INSIDE MY EXTERNAL LOADED MOVIE CLIP
// YOU CAN CALL THEM HOWEVER YOU WANT JUST AS LONG AS THEY ARE
// COMPLYING WITH NAMING SCHEMES ( NO SPACES, NO RESERVED AS3 WORDS,
// NO NUMBERS AT THE BEGINNING
// I ADD THE MouseEvent.CLICK TO THE MENU ITSELF SO I DO NOT HAVE
// TO REPEAT THE SAME CODE FOR EACH BUTTON: THE EVENT IS ONLY CALLED
// WHEN I CLICK ON THE MENU MC SO IT WILL RECOGNIZE
// THE NAMES OF EACH OF THE BUTTONS CONTAINED IN IT
MENU.addEventListener(MouseEvent.MOUSE_UP, changeMCScene);
function changeMCScene(event:MouseEvent):void {
// IT IS EASIER TO CREATE A String = text only VARIABLE
// TO STORE THE NAME OF THE BUTTONS IN IT
// I CALL IT myScene BUT YOU CAN CALL IT HOWEVER YOU WANT
var myScene:String = event.target.name;
// YOUR LOADED MC WILL CHANGE SCENE TO WHICHEVER
// BUTTON YOU CLICKED ON
// BECAUSE YOU NAMED YOUR BUTTONS AND THE SCENES
// WITH THE SAME NAMES AND YOU ARE ABLE TO RETRIEVE
// trace(myScene);
MC.gotoAndPlay(1, myScene);
// THE MENU GOES TO ITS NEUTRAL POSITION
MENU.play(); // FINISHES ANIMATION DOWN AND STOPS AT LAYER 1
}
}