Load external swf from a button in external swf and unload self?
I am an uber n00b to flash AS3. And it is not my intention to sound like a complete moron, but honestly, I have had a hard enough time figuring out just how to load an external .swf into my main file! And now my friend is asking me if I could please add a button within the external .swf which will unload itself and load a new on in its place...
I have looked around in the forums for an answer for a while now and I believe it is time for me to break down, admit defeat, and ask for help. Can someone please tell me it's not that difficult..? Surely it is possible... Thank you in advance for any advice at all! I am looking forward to the time whe开发者_JAVA技巧n I can post a response here!I have the solution! And it's only been 24 hours. I rule!
I came across dispatchEvent and that solved it!
...In case someone comes across this page seeking the answer to this seemingly-impossible-task-for-a-n00bie in the future, then they will have the answer right now! Please forgive me if I don't have the correct terminology in referencing what I did. Here we go...
In my external .swf I needed to set a listener event to my buttons (which are in my external swf). Then include a dispatchEvent within the function for my buttons . I included "true" in the arguments to 'bubble' the event back up to the main timeline. He's an example of one of my buttons.
mc_main_menu.btn_entertainment.addEventListener(MouseEvent.CLICK, button4);
function button4(event:MouseEvent) {
dispatchEvent(new Event("btn4", true));
}
Ok, but that's only half of the code to make it happen. So now, in my main swf, in which I load the external swfs into, I needed to do several things:
1). Unload the current swf from memory so that it will stop playing sounds
2). Set a listener for the dispatchEvent in my external swf (which will bubble up)
3). Load the new external swf
loader.addEventListener("btn4", page4Swf)
function page4Swf(event:Event):void{<p>
Loader(event.currentTarget).unloadAndStop(); //completely removes the swf from memory
var SWFRequest:URLRequest = new URLRequest("page4.swf");
loader.x = Xpos;
loader.y = Ypos; //these are to offset the loaded swf, if you wish
addChild(loader);
}
And there it is! I honestly didn't think that I'd be posting a response on here THAT quickly!
精彩评论