// 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;
// ESSENTIAL FOR SOUND CONTROL
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.events.MouseEvent;
////// SOUNDS >>>>>>>>>>>>>>>>
var mySound:Array = new Array();
mySound[0] = "GUETTA_01.mp3";
mySound[1] = "GUETTA_02.mp3";
mySound[2] = "GUETTA_03.mp3";
mySound[3] = "GUETTA_04.mp3";
mySound[4] = "GUETTA_05.mp3";
mySound[5] = "GUETTA_06.mp3";
mySound[6] = "GUETTA_07.mp3";
mySound[7] = "GUETTA_08.mp3";
var mySong:int = 0;// DEFAULT SONG
var reqSound:URLRequest = new URLRequest(mySound[mySong]);// DEFAULT SOUND
var sceneSound:Sound = new Sound();
sceneSound.load(reqSound);
// var volVar:Number = 0.5;// VAR FOR THE SOUND VOLUME value between 0 and 1
var volVar:Number = (FRAME.height - (FRAME.SLIDER.y + 20)) / FRAME.height;
var myVolume:SoundTransform = new SoundTransform();// value between 0 and 1
myVolume.volume = volVar;
var lastPosition:Number = 0;
/// THIS FUNCTION IS CALLED EVERY SINGLE TIME A BUTTON IS CLICKED
function playNewSound() {
reqSound = new URLRequest(mySound[mySong]);
sceneSound = new Sound(reqSound);
//volVar = (FRAME.height - (FRAME.SLIDER.y + 20)) / FRAME.height;
trace(volVar + " // " + FRAME.height + " // " + (FRAME.SLIDER.y + 20) + " // " + FRAME.SLIDER.height);
if (volVar < 0 ) { volVar = 0; }
myVolume.volume = volVar;
lastPosition = channel.position; // STORE THE POSITION / TIME OF THE MP3 WHEN PAUSED SO IT STARTS AT THE SAME TIME WHN U CLICK THE PLAY BTN
channel = sceneSound.play(lastPosition,int.MAX_VALUE,myVolume);
}
// SLIDER FOR VOLUME
// CONTROL THE SOUND WITH THE Y COORDINATES OF THE MOUSE INSIDE THE FRAME
FRAME.addEventListener(MouseEvent.MOUSE_MOVE, changeVolume);
function changeVolume(event:MouseEvent):void {
if (FRAME.mouseY < (FRAME.height - FRAME.SLIDER.height) && FRAME.mouseY > 0 ) {
volVar = (FRAME.height - (FRAME.SLIDER.y + 20)) / FRAME.height;
myVolume.volume = volVar;
// trace(myVolume.volume);
channel.soundTransform = myVolume;
FRAME.SLIDER.y = FRAME.mouseY;
}
}
// this is where everything happens:
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>STARTING MILLISECOND / LOOPS / VOLUME
var channel:SoundChannel = sceneSound.play(lastPosition,int.MAX_VALUE,myVolume);
// int.MAX_VALUE = loop forever;
// you can use any number in here and loop for 10 times for example
///////// >>>>>>>>>>>>>>>>>
// 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_TO_STAGE, ifAdded); // <<<<<< NOT Event.ADDED;
// 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);
///// ++++++++++++++++++ THIS IS NEW +++++++++++++++++++++++++++++++++++
// ADD the changeMCScene function to the MC itself so when you click;
// on the internal buttons of the Fairy Tale the SOUNDS ALSO CHANGE
// but >>> BE CAREFUL <<< WE NEED TO ADD A TESTING ELEMENT
// >>>>>>>>> event.target.parent == null <<<<<<<<<<<<<
// SO THAT THE MENU DOES NOT SHOW UP WHEN WE CLICK ON THE LOADED FAIRY TALE
MC.addEventListener(MouseEvent.MOUSE_UP, changeMCScene);
///// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// MENU IS A MOVIE CLIP PLUACED 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 {
channel.stop();
// STOP THE MUSIC;
// +++++++++++++ THIS IS NEW ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// HERE WE ARE TESTING WHETHER WE ARE CLICKING ON THE BUTTONS INSIDE THE
// FAIRY TALE OR THE ONES INSIDE THE MENU BAR
// IN ORDER TO DO THIS WE ARE GOING TO CHECK FOR THE NAME OF THE PARENT
// IT WILL BE EITHER "MENU" OR "MC" AND DEPENDING ON THE NAME WE WILL MAKE THE MENU
// PLAY OR NOT
// trace(event.target.parent);// => null when clicking the MC but not null when it's the MENU
if (event.target.parent == null || event.target.name == "MUTE" || event.target.name == "NONMUTE") {
// FINISHES ANIMATION DOWN AND STOPS AT LAYER 1;
MENU.stop();
// THE MENU GOES TO ITS NEUTRAL POSITION
} else {
MENU.play();
}
// IF YOU HAVE SOME BUTTONS ON YOUR FAIRY TALE THAT HAVE NO PURPOSE
// OR ARE JUST NOT RELATED TO NAVIGATION PURPOSES YOU WILL GET A NAVIGATION
// ERROR THAT WILL NOT BE A FATAL ERROR - Scene Not Found - BUT THE MOVIE WILL STILL
// PLAY CORRECTLY
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// 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
if (myScene == "MUTE" || myScene == "NONMUTE") {
trace("SKIP SCENE / lastPosition = " + lastPosition); // DO NOTHING
} else {
MC.gotoAndPlay(1, myScene);
}
// CONTROL THE SOUND
switch (myScene) {
case "INTRO" :
mySong = 0;
playNewSound();
break;
case "Scene1" :
mySong = 1;
playNewSound();
break;
case "Scene2" :
mySong = 2;
playNewSound();
break;
case "Scene3" :
mySong = 3;
playNewSound();
break;
case "Scene4" :
mySong = 4;
playNewSound();
break;
case "Scene5" :
mySong = 5;
playNewSound();
break;
case "CREDITS" :
mySong = 6;
playNewSound();
break;
case "CONFLICT" :
mySong = 7;
playNewSound();
break;
case "MUTE" :
volVar = 0.01;
playNewSound();
break;
case "NONMUTE" :
volVar = 0.9;
playNewSound();
break;
}
}
}