A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: load and unload movie in AS3

  1. #1
    Member
    Join Date
    Apr 2014
    Posts
    63

    load and unload movie in AS3

    I'm one of those learning to transition from AS2 to AS3. I have two buttons on stage. I used the built in code snippet for unload and load Movie. Although the issue i'm having is that if i click button 1 and then I click button 2 the first movie still stays. If I use a unload(); then I have to click the button which loads this MC twice in order to get it to load again. Anyone know what I'm not comprehending? Here is the code provided by the snippet:

    code:


    b2.addEventListener(MouseEvent.CLICK, fl_ClickToLoadUnloadSWF_2);

    import fl.display.ProLoader;
    var fl_ProLoader_2:ProLoader;

    //This variable keeps track of whether you want to load or unload the SWF
    var fl_ToLoad_2:Boolean = true;

    function fl_ClickToLoadUnloadSWF_2(event:MouseEvent):void
    {
    if(fl_ToLoad_2)
    {
    fl_ProLoader_2 = new ProLoader();
    fl_ProLoader_2.load(new URLRequest("effect.swf"));
    fl_ProLoader_2.y = 100;
    addChild(fl_ProLoader_2);
    }
    else
    {
    fl_ProLoader_2.unload();
    removeChild(fl_ProLoader_2);
    fl_ProLoader_2 = null;
    }
    // Toggle whether you want to load or unload the SWF
    fl_ToLoad_2 = !fl_ToLoad_2;
    }


    btn.addEventListener(MouseEvent.CLICK, fl_ClickToLoadUnloadSWF_5);

    import fl.display.ProLoader;
    var fl_ProLoader_5:ProLoader;

    //This variable keeps track of whether you want to load or unload the SWF
    var fl_ToLoad_5:Boolean = true;

    function fl_ClickToLoadUnloadSWF_5(event:MouseEvent):void
    {
    if(fl_ToLoad_5)
    {
    fl_ProLoader_5 = new ProLoader();
    fl_ProLoader_5.load(new URLRequest("effect2.swf"));
    fl_ProLoader_5.y = 150;
    addChild(fl_ProLoader_5);
    }
    else
    {
    fl_ProLoader_5.unload();
    removeChild(fl_ProLoader_5);
    fl_ProLoader_5 = null;
    }
    // Toggle whether you want to load or unload the SWF
    fl_ToLoad_5 = !fl_ToLoad_5;
    }




    S

  2. #2
    Member
    Join Date
    Apr 2014
    Posts
    63
    I almost got it working. I changed it to the below and b2 unloads the movie loaded by btn but btn does not unload the movie loaded by b2. Anyone know why?

    code:



    btn.addEventListener(MouseEvent.CLICK, fl_ClickToLoadUnloadSWF_5);

    import fl.display.ProLoader;
    var fl_ProLoader_5:ProLoader;
    var tt:Number;

    function fl_ClickToLoadUnloadSWF_5(event:MouseEvent):void
    {
    if(ss == 9) {
    fl_ProLoader_2.unload();
    fl_ProLoader_5.load(new URLRequest("effect2.swf"));
    fl_ProLoader_5.y = 150;
    addChild(fl_ProLoader_5);
    }else if (ss != 9){
    tt = 5;
    fl_ProLoader_5 = new ProLoader();
    fl_ProLoader_5.load(new URLRequest("effect2.swf"));
    fl_ProLoader_5.y = 150;
    addChild(fl_ProLoader_5);
    }

    }

    b2.addEventListener(MouseEvent.CLICK, fl_ClickToLoadUnloadSWF_2);

    import fl.display.ProLoader;
    var fl_ProLoader_2:ProLoader;
    var ss:Number;

    function fl_ClickToLoadUnloadSWF_2(event:MouseEvent):void
    {
    if(tt == 5) {
    fl_ProLoader_5.unload();
    fl_ProLoader_2 = new ProLoader();
    fl_ProLoader_2.load(new URLRequest("effect.swf"));
    fl_ProLoader_2.y = 100;
    fl_ProLoader_2.x = 100;
    addChild(fl_ProLoader_2);
    }else if(tt != 5){
    ss = 9;
    fl_ProLoader_2 = new ProLoader();
    fl_ProLoader_2.load(new URLRequest("effect.swf"));
    fl_ProLoader_2.y = 100;
    fl_ProLoader_2.x = 100;
    addChild(fl_ProLoader_2);
    }

    }


  3. #3
    Member
    Join Date
    Apr 2014
    Posts
    63
    Here is the error

    TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at LoadMovie_fla::MainTimeline/fl_ClickToLoadUnloadSWF_5()

    btn is not loading the movie but b2 is working properly. I've triple checked them both and don't see any differences between the two.
    Last edited by sandman99; 05-22-2014 at 03:27 AM.

  4. #4
    Senior Member
    Join Date
    Nov 2001
    Posts
    1,145
    I didn't go through all the code, just the first. You were trying to have the clip remove itself. Manage the adding and removing of the clip separately. Have a button clicked function call another function to remove that button.

    Here is an example AS3 I made for guys that are in AS2. You can see how you can handle buttons and how to lay out your flash in general.
    Attached Files Attached Files

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center