2

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

1 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks , however I now get this error: TypeError: Error #1009: Cannot access a property or method of a null object reference. at map_fla::MainTimeline/frame1()
Did you add the definition for the singleLoader? Like so "var singleLoader:Loader = new Loader();" ??
Ah I was missing that and its resolved the problem, however now it still loads the movie on top of the original and shows a mix of the two. I've tried both of your suggestions.
Yes I am , however I'm also displaying a map using the google api. There must be someway to clear the screen and just load the second swf, even if you can open it up in a new window perhaps?
//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("slam.swf"); singleLoader.unloadAndStop(true); singleLoader.load(request); while(stage.numChildren > 0) { removeChildAt(0); } stage.addChild(singleLoader); } Gives me the following error now : RangeError: Error #2006: The supplied index is out of bounds. Many thanks for your help so far, much appreciated
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.