How to make Flash 'play well with others'?

Posted by Sensei James on Stack Overflow See other posts from Stack Overflow or by Sensei James
Published on 2010-04-21T08:37:16Z Indexed on 2010/04/21 8:43 UTC
Read the original article Hit count: 383

What up fam. So this isn't a question asking about memory management schemes; for those of you who may not know, the Flash Virtual Machine relies on garbage collection by using reference counting and mark and sweep (for good coverage of these topics, check out Grant Skinner's article and presentation). And yes, Flash also provides the "delete" operator, which can (unfortunately only) be used to remove the properties of dynamic objects.

What I want to know is how to make it so that Flash programs don't continue to consume CPU and memory while running in the background (save loading content or communicating remotely, for example). The motivation for this question comes in part from Apple's ban on cross compiled applications (in its SDK 4) on the grounds that they do not behave as predicted with the multitasking feature central to iPhone OS 4. My intention is not only to make Flash programs that will 'pass muster' as far as multitasking in iPhone OS 4, but also to simply make better (behaving) Flash programs.

Put another way, how might a Flash application mimic the multitasking feature of iPhone OS 4? Does the Flash API provide the means for a developer to put their applications to 'sleep' while other programs run, and then to 'awaken' them just as quickly?

In our own program, we might do something as crude as detecting when the user has been idle (no mouse motion or key press) for (say) four seconds:

var idle_id:uint = setInterval(4000, pause_program);
var current_movie_clip:MovieClip;
var current_frame:uint;

...

// on Mouse move or key press...
clearInterval(idle_id);
idle_id = setInterval(4000, pause_program);

...

function pause_program():void
{
 current_movie_clip = event.target as MovieClip;
 current_frame = current_movie_clip.currentFrame;
 MovieClip(root).gotoAndStop("program_pause_screen");
}

(on the program pause screen)

resume_button.addEventListener(MouseEvent.CLICK, resume_program);

function resume_program(event:MouseEvent)
{
 current_movie_clip.gotoAndPlay(current_frame);
}

If that's the right idea, what's the best way to detect that an application should be shelved?

And, more importantly, is it possible for Flash Player to detect that some of its running programs are idle, and to similarly shelve them until the user performs an action to resume them?

(Please feel free to answer as much or as little of the many questions I've posed.)

© Stack Overflow or respective owner

Related posts about flash

Related posts about memory-management