Skip to main content
Inspiring
October 13, 2009
Answered

Loading External SWF Files: Making Things Simple...

  • October 13, 2009
  • 1 reply
  • 672 views

Hello everyone! This is my first post and I'm learning AS3 😃

I didn't know about this forum but now I hope to have the time to come hear a lot, to help and be helped!

Well, I'm trying to import some external files to my main flash file with buttons. Yes, this is a newbie question, but I'm learning... I click button 1 and content 1 is loaded, click button 2 and content 2 is loaded, and so on. So, I got two ways for doing it:

**various buttons with listeners to run the function "loadCity"

var box:MovieClip = new MovieClip();
addChild(box);
box.x=20;
box.y=20;

function loadCity (Event:MouseEvent):void {
var currentCity=Event.target.name;
var swfRequest:URLRequest=new URLRequest(currentCity+".swf");
var swfLoader:Loader = new Loader();
swfLoader.load(swfRequest);
box.addChild(swfLoader); }

The problem with this first option is that the content to the box is not renew with each click but it stacks one over each other, so I can't use it =/

import fl.transitions.Tween;
import fl.transitions.easing.*;

var box:MovieClip = new MovieClip();
addChild(box);
box.x=20;
box.y=20;

var cityLoader:Loader = new Loader();
var cityURL:URLRequest=new URLRequest("montreal.swf");
cityLoader.load(cityURL);
box.addChild(cityLoader);

**various buttons with listeners to run it's own functions, like "montreal.addEventListener(MouseEvent.CLICK, loadMontreal);"

var box:MovieClip = new MovieClip();
addChild(box);
box.x=20;
box.y=20;

function loadCity (Event:MouseEvent):void {
var currentCity=Event.target.name;
var swfRequest:URLRequest=new URLRequest(currentCity+".swf");
var swfLoader:Loader = new Loader();
swfLoader.load(swfRequest);
box.addChild(swfLoader); }

The problem with this first option is that the content to the box is not renew with each click but it stacks one over each other, so I can't use it =/

This topic has been closed for replies.
Correct answer Andrei1-bKoviI

You can remove all the children of the box before placing another object into it:

function loadCity (event:MouseEvent):void {
     var currentCity = event.target.name;
     // remove all the children of the box
     while (box.numChildren > 0) {
          box.removeChildAt(0);
     }
     var swfRequest:URLRequest = new URLRequest(currentCity + ".swf");
     var swfLoader:Loader = new Loader();
     swfLoader.load(swfRequest);
     box.addChild(swfLoader);
}

I see that you intend to use tween - in this case above code will not allow you to perform transitions graciously...

1 reply

Andrei1-bKoviICorrect answer
Inspiring
October 13, 2009

You can remove all the children of the box before placing another object into it:

function loadCity (event:MouseEvent):void {
     var currentCity = event.target.name;
     // remove all the children of the box
     while (box.numChildren > 0) {
          box.removeChildAt(0);
     }
     var swfRequest:URLRequest = new URLRequest(currentCity + ".swf");
     var swfLoader:Loader = new Loader();
     swfLoader.load(swfRequest);
     box.addChild(swfLoader);
}

I see that you intend to use tween - in this case above code will not allow you to perform transitions graciously...

newToAS3Author
Inspiring
October 13, 2009

Thanks Andrei!

The "while statement" did the trick! =D

Just one little problem. This first thread was opened involuntarily and I don't know how to delete it. The other topic, above this one, is more complete and has more details in what I'm trying to do. I still want to use tweens. =/ Could you check my other topic?

Well, thanks again Andrei and I'm leaving the finished code here, just in case anyone else needs to do the same thing. =)

montreal.addEventListener(MouseEvent.MOUSE_UP, loadCity);
dublin.addEventListener(MouseEvent.MOUSE_UP, loadCity);
sydney.addEventListener(MouseEvent.MOUSE_UP, loadCity);
...

var box:MovieClip = new MovieClip();
addChild(box);
box.x=20;
box.y=20;

function loadCity (event:MouseEvent):void {
    var currentCity = event.target.name;
    // remove all the children of the box
    while (box.numChildren > 0) {box.removeChildAt(0);}
    var swfRequest:URLRequest=new URLRequest(currentCity+".swf");
    var swfLoader:Loader = new Loader();
    swfLoader.load(swfRequest);
    box.addChild(swfLoader);}