Multi-Treading Oriented ActionScript
I am currently working on a Flash animation that must deal with simultaneous management of dynamically created, autonomous MovieClips and the management of data obtained from various XML feeds from the Internet. It’s basically a RSS Aggregator in which RSS items are represented by a graphical symbol. All must be generated using ActionScript, meaning the actual .swf file only consists of a call to the constructor of my main class.
Having learned how to program mainly with Java and VB.Net, the approach I wanted to take was to have two threads running in my flash movie : one that fetches and treats the XML data, the second that handles events, collisions and movements of the MovieClips. However, to put it bluntly, there is no such thing as multi-treading in Flash 9.
The approach I chose to solve this problem was to generate calls to key function of my main class using the setInterval function. Of course, on a much larger scale, one would probably notice a lag when using an interval technique compared to real simultaneous treading. However, with the usual Flash applications this kind of performance goes rather unnoticed.
Here’s an example similar to what I am doing:
class Feed {
public function Feed():Void {
setInterval(this.refreshFeedData, 500);
setInterval(this.moveFeedMovieClips, 500);
}
public function refreshFeedData():Void {
// load the xml
}
public function moveFeedMovieClips():Void {
// run through my MovieClip objects
}
}
By keeping a frequent and similar interval (in this example 0.5 seconds), you can mimic the threading effect. Note that you can lose the reference to the class using “this.” in the function being called by setInterval . I have yet to understand and explain this problem, but you can go around it by supplying a reference to the current class in the call :
setInterval(this.moveFeedMovieClips, 500, this);