Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

AS3 button help

Guest
Nov 09, 2015 Nov 09, 2015

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.

TOPICS
ActionScript
469
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Nov 09, 2015 Nov 09, 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 th

...
Translate
LEGEND ,
Nov 09, 2015 Nov 09, 2015

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Nov 09, 2015 Nov 09, 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);

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 09, 2015 Nov 09, 2015
LATEST

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 09, 2015 Nov 09, 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".

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines