Skip to main content
This topic has been closed for replies.
Correct answer sinious

Few quick things.. You're running loadScene() right in a frame script which is typically fine but just for safety it's often advised to wait until entering a frame to fire off your first script, especially when it accesses the stage.

e.g.

addEventListener(Event.ENTER_FRAME, onEF);

function onEF(e:Event):void {

     removeEventListener(Event.ENTER_FRAME, onEF);

     // stage is definitely ready here, run the function

     loadScene();

}

Outside that, you're not doing any error checking on the Loader's request. You can find plenty of examples of checking for all sorts of load errors on the Loader page:

Loader - Adobe ActionScript® 3 (AS3 ) API Reference

Specifically keep in mind you add the error listeners to the .contentLoaderInfo property of the Loader object myLoader:

.contentLoaderInfo

After that, your button code in backButtonClick() function is making a new URLRequest, asking the loader "singleLoader" you made to load it and then is running addChild(myLoader) again instead of the "singleLoader". If your intention is literally to reload it, you should just be using "myLoader" again rather than "singleLoader".

2 replies

sinious
siniousCorrect answer
Legend
November 9, 2015

Few quick things.. You're running loadScene() right in a frame script which is typically fine but just for safety it's often advised to wait until entering a frame to fire off your first script, especially when it accesses the stage.

e.g.

addEventListener(Event.ENTER_FRAME, onEF);

function onEF(e:Event):void {

     removeEventListener(Event.ENTER_FRAME, onEF);

     // stage is definitely ready here, run the function

     loadScene();

}

Outside that, you're not doing any error checking on the Loader's request. You can find plenty of examples of checking for all sorts of load errors on the Loader page:

Loader - Adobe ActionScript® 3 (AS3 ) API Reference

Specifically keep in mind you add the error listeners to the .contentLoaderInfo property of the Loader object myLoader:

.contentLoaderInfo

After that, your button code in backButtonClick() function is making a new URLRequest, asking the loader "singleLoader" you made to load it and then is running addChild(myLoader) again instead of the "singleLoader". If your intention is literally to reload it, you should just be using "myLoader" again rather than "singleLoader".

Ned Murphy
Legend
November 9, 2015

You should include the code in your posting.  Some people will not download files for a variety of good reasons.

November 9, 2015

Here is the code:

stop();

var myLoader:Loader;

function loadScene():void {

    if (myLoader) {

        myLoader.unloadAndStop();

        removeChild(myLoader);

    }

    myLoader = new Loader();                  

    var url:URLRequest = new URLRequest("scene.swf");

    myLoader.load(url);                                  

    addChild(myLoader);                 

    // (optional)

    myLoader.x = 30;      

    myLoader.y = 30;

}

loadScene(); // call now to start, then call again in button code

//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("scene.swf");

    singleLoader.unloadAndStop(true);

    singleLoader.load(request);

    addChild(myLoader);

}

sinious
Legend
November 9, 2015

You can always just point the button code right back to loadScene():

function backButton(e:MouseEvent) {

     loadScene();

}

It unloads, removes from stage (doesn't need to but no biggie) and handles the rest of the same request.