Copy link to clipboard
Copied
I'm having some trouble getting a button to load and unload an incoming swf file.
http://www.scottberks.com/clientwork/flash/flash.zip
I'm using CS6.
Any help would be REALLY appreciated.
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 th
...Copy link to clipboard
Copied
You should include the code in your posting. Some people will not download files for a variety of good reasons.
Copy link to clipboard
Copied
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);
}
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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:
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".
Find more inspiration, events, and resources on the new Adobe Community
Explore Now