PDA

View Full Version : How to internally close an swf program


dcster2001
04-25-2008, 02:41 PM
I started this from a reply in a related thread because it is a slightly different topic.

It may be obvious, but I can't seem to find it: is it possible for a swf to programmatically exit/close itself via an exit button on the swf? If so, how would you write the code?

I am writing an AS3 program that I want to include an exit button that warns the user if he hasn't saved his data before closing the program, something like this:

ActionScript Code:
private function onExit_click (e:MouseEvent):void {
if (unloggedData) {
warn();
}
else {
exit();
}
}

It's the exit() part that has me stumped-- what is the actual command to close the program?

dcster2001
04-25-2008, 10:26 PM
I have wracked my brains on this one. I cannot find anything in the Flash Help manual, ActionScript 3.0 Bible, Moock's Essential ActionScript3.0, or the ActionScript 3.0 Cookbook on how to close or monitor the closing of a swf from within itself.

In short, I am stumped.

Is there no way to make an exit button in an swf that exits itself like the little red "X" close button in a Windows program?

It seems like Flash programs should be able to at least listen to a close command so that you can ensure that the user has completed a save before closing.

joshstrike
04-26-2008, 07:25 AM
If this isn't an AIR app, just an .swf that you're embedding in an HTML page, there's no way for it to close itself. HTML is a static document format and whatever is sent down from the server to the browser window becomes the window's content, embed tags and all. The only way to alter the content of the browser window after it's been loaded up is to use javascript and rearrange the elements of the Document Object Model for the page. So an SWF could appear to "close itself" by keeping your embed tag inside a div on the HTML page and writing a javascript function that changed the content of the div, and then calling that javascript function from Flash. This works if you have complete control over the page, set allowScriptAccess="always" in your embed tag, and your users have javascript enabled. For that matter, you could even have the JS function close the whole browser window. But you have to think of your Flash code as existing within its own virtual machine, which starts running as soon as your browser sees the embed tag. You can end your program any way you want, but the virtual machine -- and the space occupied by the embed tag -- will still be sitting on the web page.

dcster2001
04-27-2008, 02:42 AM
Thanks for the reply!

So, if you are making a Flash Projector app (something I've not yet done, so I don't know how it exactly works), the only way the thing ends is for a user to close the Flash player itself, and you can't monitor the closing action in order to ensure that the user has saved his info, right?

I guess the work around is to provide an exit button that "pretends" to end the program and then grays the screen out or something, but doesn't actually close the program, and suggests to the user that he close the player/browser via its own mechanism.

Shame, really. I was hoping to make my program act like a regular Windows program, but then again, I guess I should be learning C++ or something like it then, if I want that sort of functionality.;)