Lu Aye's
Blog, images and projects: Feed
LuAye.com
RSS Feed 
| All | Life | Ideas and thoughts | Reviews | Shout out | News | Coding |
<Previous Post Next Post>
Threaded processing in Flash AS3
  Coding
Wrote a class which handles faked multi threading (by spread threading) for Flash AS3.
What is it?:

Flash runs in a single thread. Which means if you execute an intensive script, flash would lag/un-respond until your script finishes. You will want to spread the process over a number of frames to avoid this lag.
This class allows you to call a function over and over again while keeping the frameRate and interface response fairly stable and also avoid falling into script timeout.


Example use:

I used this for 'path finding' which can take between 10ms to 1000ms (depending on map size).
I don't want user to lag out every time they choose a path. Instead I trade it off by taking 1-10 frames for path to be found.

In an arcade style game, I needed to pre render the background + static objects into tiled bitmaps. Some long levels takes quite a long time to render so using Thread lets me spread the process and have a smoother 'building' screen animation (rather than a stucked/lagged screen).


When you shouldn't use:
- If you want the outcome of the process straight away, this will not always delivery - because it will spread your function calls over a number of frames if needed.


To be careful:
- This class DOES NOT stop the function from excuting half way through. It only spreads the time between each function call (when needed).
- If your process is not only a 'loop' you could make a function that manage the sequence in which it should execute by looking at 'steps' to know which step of thread you are on.
- You can call multiple Threads to execute at the same time - However each thread does not share 'givenTime' with others so you will need to manage it for better processing spread.


Example Code:

Example below is using Thread to increament an integer 'n' up to 500,000
giving 20 ms per frame to excute (it let go a frame when 20ms is passed).
Change countUpTo var if you are getting 1-2 frames/threadStep - your machine must be fast then!


import com.luaye.utils.thread.Thread;

var countUpTo:int = 500000;
var traceEvery:int = 20000;

new Thread(stepFun, {n:0}, false, 20);

function stepFun(thread:Thread):void{
var vars:Object = thread.vars;
vars.n++;
if(vars.n%traceEvery == 0){
trace("n = ", vars.n, ", threadStep = ", thread.threadSteps);
}
if(vars.n >= countUpTo){
// need to call end() to stop executing.
thread.end();
trace("Ended process after "+thread.threadSteps+" frames/threadSteps");
}
}


Source: Click here

Happy coding!

  Post a Comment
Name :
Email :
Comment :
 
Comments: 1
  Hadi
How can I read this and not be happy? I had a lot of trouble with having my flash freeze before executing all different heavy function... this method will most probably solve the problem :) thanks... here is the app I'm working on for your reference: http://emstris.com/sample/slushh/July05_mainApp/