简体   繁体   中英

Loading another swf file using AS3

I'm trying to load in another swf on a button click using Aaction Script 3.

The problem I'm having is that it just seems to load and mix the movies together. Is is possible to load and replace on stage the newly loaded swf similar to how you could do this in AS2 using loadMovieNum()

This is what I have so far:

//Add event listener for button click
backButton.addEventListener(MouseEvent.CLICK, backButtonClick);

//Create a function for the button click
function backButtonClick(ev:MouseEvent):void
{
var request:URLRequest = new URLRequest("2.swf");
var loader:Loader = new Loader()
loader.load(request);
addChild(loader);
}

Many thanks

Use the loader like this:

//Add event listener for button click
var singleLoader:Loader = new Loader();
backButton.addEventListener(MouseEvent.CLICK, backButtonClick);

//Create a function for the button click
function backButtonClick(ev:MouseEvent):void
{
    var request:URLRequest = new URLRequest("2.swf");
    singleLoader.load(request);        
    addChild(loader);
}

What you're doing is you're creating a new Loader every single time for every new SWF you're loading. Just use a single loader and each time you load content on it, it should replace the existing content. If not, adjust the code like so:

function backButtonClick(ev:MouseEvent):void
{
    var request:URLRequest = new URLRequest("2.swf");
    singleLoader.unloadAndStop(true);
    singleLoader.load(request);
    addChild(loader);
}

See the documentation for more: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/Loader.html

Update

If you want to clear everything off the stage when you do this, see the following question & answer My Actionscript 3.0 Stage will not clear .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM