Search code examples
actionscript-3flashactionscriptflash-builderflash-cs6

AS3 - Nested SWF Button Problems


So I have a main SWF as a main menu that launches other SWFs and it launches fine but when the other apps are running you can still click the buttons that were on the main menu...

function startLoad(e:MouseEvent){
    var mLoader:Loader = new Loader();
    var mRequest:URLRequest;

    if (e.target == btnOne){
        mRequest = new URLRequest("appOne.swf");
    }
    else if (e.target == btnTwo){
        mRequest = new URLRequest("appTwo.swf");
    }

    mLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onCompleteHandler);
    mLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgressHandler);
    mLoader.load(mRequest);
}

I can disable the main menu buttons and that works but so far I haven't found a way to trigger the main menu to re-enable them.


Solution

  • Disabling the button you obtained from e.target when event arrives into startLoad method will improve the behaviour of your code. Then differentiating the onCompleteHandler methods per swf will give you the chance to enable the corresponding buttons back.

    Since I know nothing about your button class, so I call it YourButtonClass, I will write disable(); and enable(); in the examples below for disabling and enabling methods for the buttons. Please replace them with the appropriate correct class name methods or property setting. Also checking for the e.target class and buttons will avoid unnecessary tragedies.

    function startLoad(e:MouseEvent){
    var mLoader:Loader;      // we havent seen the river, lets not inflate our boat.
    var mRequest:URLRequest;
    
    if(!(e.target is YourButtonClass)) return;            // no nightmares..
    if((e.target != btnOne)&&(e.target != btnTwo))return; // no nightmares..
    YourButtonClass(e.target).disable();                  // disable the button here
    mLoader = new Loader(); // river! inflate the boat :)
    if (e.target == btnOne){
        mRequest = new URLRequest("appOne.swf");
        mLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onCompleteAppOne);
    }
    else { // we are sure it is btnTwo if not btnOne now...
        mRequest = new URLRequest("appTwo.swf");
        mLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onCompleteAppTwo);
    }    
    mLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgressHandler);
    mLoader.load(mRequest);
    }
    
    // this method is for enabling btnOne
    protected function onCompleteAppOne(Event: e){ 
        btnOne.enable();
        commonCompleteOperations(e);// if you have other operations post processing
    }
    
    // this method is for enabling btnTwo
    protected function onCompleteAppTwo(Event: e){ 
        btnTwo.enable();
        commonCompleteOperations(e);// if you have other operations post processing
    }   
    
    // this method is for on complete common operations if you have.
    protected function commonCompleteOperations(Event e){
        // do some processing here, for instance remove event listener check for
        // application domain etc...
    }
    

    As a precaution, I would listen to security error and io error events. Both error events can be handled by a single handler method per button/file.