PDA

View Full Version : AS3 help with understanding loaders and remove Children



dList
June 8th, 2008, 05:18 PM
Hi All,
I am very new, and not very good at Actionscript 3. I am using a bunch of different loaders to load swf's (via my nav.as file) into a Main.fla on a button CLICK event. I have to use different loaders, because the swf's are different sizes.

I am able to load everything, but having trouble figuring out how to remove those children. The code is a bit cumbersome because that's the only way I could make it function.The parent is the AssetLoader, the parent.parent is the object Main, and the parent.parent.parent is the object stage...

I will put my Navigation.as code here but I have removed a lot of it, so it would not work if I attach the fla... I can send a stuffed file if that's needed.

Does anyone have any ideas for me? Thank you very much in advance...


package {
(yes, I am importing everything I need here - just skipped this to save space)

public class Nav extends MovieClip {

var button:BlueButton;
var btnArray:Array = new Array();
var ball:NavBall;
var mySound:Sound;
var request:URLRequest;
var loader:Loader = new Loader();

public function Nav():void { //loading navigation buttons here
for (var i:int = 0; i < 7; i++) {
button = new BlueButton;
addChild(button);
button.x = 0;
button.y = 0 + i * 59;
button.name = "button" + i;
btnArray.push(button.name);
button.addEventListener(MouseEvent.MOUSE_OVER, Highlight);
button.addEventListener(MouseEvent.MOUSE_OUT, Normal);
button.addEventListener(MouseEvent.CLICK, onClick);
}
// ---had to add text individually to buttons--- // so it didn't take on behaviors
var box1 = new TextBox("upcoming events");
addChild(box1);
box1.x = 8;
box1.y = 7;
var box2 = new TextBox("trivia & contest day");
addChild(box2);
box2.x = 8;
box2.y = 66;
var box3 = new TextBox("schedules & locations");
addChild(box3);
box3.x = 8;
box3.y = 125;
//etc. more buttons written the same way...
}
//...skipping some functions that work fine...

public function onClick(e:MouseEvent):void {
removeAllChildren();
if(e.target.name == "button0") {
loader.load(new URLRequest("CONTENT/ContentEvents.swf"));
parent.addChild(loader);
loader.x = 195;
loader.y = 5;
var loaderBanner:Loader = new Loader();
loaderBanner.load(new URLRequest("Banner.swf"));
parent.addChild(loaderBanner);
loaderBanner.x = 0;
loaderBanner.y = -218;
}
else if(e.target.name == "button1") {
removeAllChildren();
var loader2:Loader = new Loader();
loader2.load(new URLRequest("CONTENT/ContentTrivia.swf"));
parent.addChild(loader2);
loader2.x = 195;
loader2.y = 5;
//second button gallery - need to get content
}
else if(e.target.name == "button2") {
var loader3:Loader = new Loader();
loader3.load(new URLRequest("CONTENT/ContentSchedule.swf"));
parent.addChild(loader3);
loader3.x = 195;
loader3.y = 5;
var galleryLoader1:Loader = new Loader();
galleryLoader1.load(new URLRequest("Gallery1.swf"));
parent.addChild(galleryLoader1);
galleryLoader1.x = 0;
galleryLoader1.y = -218;
}

//etc. there are 6 buttons
//I have removed the rest of the loaders code...all written the same
}

function removeAllChildren():void { //this removes everything from the main stage including stuff loading from elsewhere
if (parent.parent.numChildren > 0) {
removeChild(parent.parent.numChildren-1);
}
}
}

Garfty
June 8th, 2008, 06:10 PM
have you tried using the removeChildAt function??

dList
June 8th, 2008, 06:46 PM
have you tried using the removeChildAt function??
Thanks for responding,

Do you mean in my removeAllChildren function? I changed it to read removeChildAt(parent.parent.numChildren-1);
however, it doesn't remove anything.

if it reads
removeChildAt(parent.numChildren-1);
it starts removing the buttons themselves...

if I leave the (-1) off it doesn't remove anything.

I must not be understanding where the children are... or do you think the problem might be due to all the different loaders?