-
Removing Loaded swf...
Hi Im currently working on a flash website and am needing help with removing swf files that I import onto the main stage at the click of a button, So basically I want to unload the previous page before it loads the new page here is my code:
Code:
var mov1Request:URLRequest = new URLRequest("1.swf");
var mov2Request:URLRequest = new URLRequest("2.swf");
var mov3Request:URLRequest = new URLRequest("3.swf");
var mov4Request:URLRequest = new URLRequest("4.swf");
var mov5Request:URLRequest = new URLRequest("5.swf");
var mov6Request:URLRequest = new URLRequest("6.swf");
var mov7Request:URLRequest = new URLRequest("7.swf");
var mov8Request:URLRequest = new URLRequest("8.swf");
var mov1Loader:Loader = new Loader();
var mov2Loader:Loader = new Loader();
var mov3Loader:Loader = new Loader();
var mov4Loader:Loader = new Loader();
var mov5Loader:Loader = new Loader();
var mov6Loader:Loader = new Loader();
var mov7Loader:Loader = new Loader();
var mov8Loader:Loader = new Loader();
Button1.addEventListener(MouseEvent.CLICK, click1);
Button2.addEventListener(MouseEvent.CLICK, click2);
Button3.addEventListener(MouseEvent.CLICK, click3);
Button4.addEventListener(MouseEvent.CLICK, click4);
Button5.addEventListener(MouseEvent.CLICK, click5);
Button6.addEventListener(MouseEvent.CLICK, click6);
Button7.addEventListener(MouseEvent.CLICK, click7);
Button8.addEventListener(MouseEvent.CLICK, click8);
mov1Loader.load(mov1Request)
Background.addChild(mov1Loader);
function click1(event:MouseEvent):void{
mov1Loader.load(mov1Request)
Background.addChild(mov1Loader);
}
function click2(event:MouseEvent):void{
mov2Loader.load(mov2Request)
Background.addChild(mov2Loader);
}
function click3(event:MouseEvent):void{
mov3Loader.load(mov3Request)
Background.addChild(mov1Loader);
}
function click4(event:MouseEvent):void{
mov4Loader.load(mov4Request)
Background.addChild(mov4Loader);
}
function click5(event:MouseEvent):void{
mov5Loader.load(mov5Request)
Background.addChild(mov5Loader);
}
function click6(event:MouseEvent):void{
mov6Loader.load(mov6Request)
Background.addChild(mov6Loader);
}
function click7(event:MouseEvent):void{
mov7Loader.load(mov7Request)
Background.addChild(mov7Loader);
}
function click8(event:MouseEvent):void{
mov8Loader.load(mov8Request)
Background.addChild(mov8Loader);
}
Hope someone can help! :)
-
First off, you type in 6 times everything for each swf, you can , and you can make this considerably shorter (and easier to read) by using only one loader, and processing them one after the other, rather than all at once:
PHP Code:
var currSWF:uint = 1;
var movLoader:Loader;
var swfs:Array = new Array();
function loadSWF():void
{
movLoader = new Loader();
movLoader.load(new URLRequest(currSWF + ".swf")); // will load in one SWF
moveLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, SWFComplete);
currSWF++; // increace one time for the next round
}
function SWFComplete(e:Event):void
{
var swf:MovieClip = e.currentTarget.content;
swfs.push(swf); // storing them, not adding them -> easier later
if(currSWF < 8) loadSWF(); // go for another round
else
{
activateBtns(); // everything is loaded, time to activate the btns
currSWF = 0;
}
}
function activateBtns():void
{
var currBtn:uint = 1;
for(var i:uint = 0; i < 8; i++)
{
this["Button" + currBtn].addEventListener(MouseEvent.CLICK, clickHandler);
currBtn++;
}
}
function clickHandler(e:MouseEvent):void
{
// since your btns include a number in their name, we need the name
var btnName:String = String(e.currentTarget.name);
// the number is the last character, but we'll need the uint version of it, not String
var index:uint = uint(btnName.charAt(btnName.length - 1));
if(index != currSWF) // if the index doesn't match the currSWF variable
{
if(currSWF == 0) addChild(swfs[index - 1]); // there's no swf displaying, so just add it
else
{
removeChild(swfs[currSWF - 1]); // remove the current one
addChild(swfs[index - 1]); // and add the new one
}
}
currSWF = index; // this way we keep track of which swf is shown
}
So that will make it tidier and normally it should remove the one showing before adding another one.
Haven't tested it yet, so if you get any errors, please post them :)
-
ok so it loads with no errors but does nothing when hitting the buttons.
I had for got to mention before that my SWF files acctualy have different names eg. main page, pictures, media etc.. and the actions are on the first frame of the first layer.
Im not sure how this code will help with switching back and forth through pages.
-
Ow, yeah, that will be a problem indeed.. I just wanted to clear a bit of lines for you ;)
But the way this should be solved will probably be something like I used.
Use a variable to store the current swf. When clicking a button, check to see if the requested swf isn't already there, and if not, remove it by removing the reference variable (probably also of the type Loader in your case) and adding the new Loader (then set the reference variable equal to the new swf to continue the loop with the next click)
-
Ok im still confused at how to solve this, AS2 was so much easier...could you give any example of code in how to do this?
-
For the different names you can use an Array:
PHP Code:
var currSWF:uint = 1;
var movLoader:Loader;
// so store the names in here, no .swf needed
var swfNames:Array = ["firstName","secondDifferentName","otherTitle","someSWF"];
var swfs:Array = new Array();
function loadSWF():void
{
movLoader = new Loader();
// we take one of the objects in the Array using the currSWF variable as index
movLoader.load(new URLRequest(swfNames[currSWF] + ".swf")); // will load in one SWF
moveLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, SWFComplete);
currSWF++; // increace one time for the next round
}
function SWFComplete(e:Event):void
{
var swf:MovieClip = e.currentTarget.content;
swfs.push(swf); // storing them, not adding them -> easier later
if(currSWF < 8) loadSWF(); // go for another round
else
{
activateBtns(); // everything is loaded, time to activate the btns
currSWF = 0;
}
}
Should work I guess :)
Are your buttons on that same frame as where you put the code? Try putting a trace inside of clickHandler() I posted a while ago and comment out all the other code in there to see if they really aren't doing anything.
EDIT: If your buttons have different names than Button0, Button1, etc than you'll have to use another Array as I used above for the buttons too!