Posts Tagged Flash

Background Processing Class in AS3

I’ll be trying to release some useful classes over the next couple of months, many of which I used to build Rainbow. So starting off here’s a class that became pretty handy when running a function that chewed up lots of CPU time.

Background.as
This is a simple background process manager class that uses up a specific ratio of CPU time to process functions that are registered to it. For instance if you have to generate a whole bunch of sprites, but don’t want the .swf to freeze while the number crunching happens, you can register a sprite creation function that will create one sprite at a time. on Each ENTER_FRAME event the Background class will continue to call its registered functions until all of it’s alloted CPU time has been used.

Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
//**********************************
//
// © 2009 - James McNess
// <a href="http://www.codeandvisual.com">http://www.codeandvisual.com</a>
//
// **********************************
 
package {
 //
 // *******************************************************************
 //
 //
 // IMPORTS
 //
 //
 // *******************************************************************
 import flash.display.*
 import flash.events.*
 import flash.utils.*
 public class Background{
  //
  // *******************************************************************
  //
  //
  // PROPERTIES
  //
  //
  // *******************************************************************
  private var pFrameMonitor:DisplayObject
  private var pCPURatio:Number
  //
  private var pIdleLength:Number
  private var pFunctions:Array
  //
  //
  // *******************************************************************
  //
  //
  // INITIALISATION
  //
  //
  // *******************************************************************
  public function Background(thisFrameMonitor:DisplayObject, thisCPURatio:Number){
   pFrameMonitor = thisFrameMonitor
   pCPURatio = thisCPURatio
   init()
  }
  private function init(){
   pFunctions = new Array()
   pIdleLength = 1000/pFrameMonitor.stage.frameRate*pCPURatio
   pFrameMonitor.addEventListener(Event.ENTER_FRAME,doIdle)
  }
  private function doIdle(evt:Event){
   var myStartTime:Number = getTimer()
   while(getTimer()-myStartTime&lt;pIdleLength&amp;&amp;pFunctions.length&gt;0){
    for(var i in pFunctions){
     var myFunction:Function = pFunctions[i]
     myFunction()
    }
   }
  }
  //
  //
  // *******************************************************************
  //
  //
  // API
  //
  //
  // *******************************************************************
  public function addFunction(thisFunction:Function){
   if(pFunctions.indexOf(thisFunction)==-1){
    pFunctions.push(thisFunction)
   }
  }
  public function removeFunction(thisFunction:Function){
   var myIndex:int = pFunctions.indexOf(thisFunction)
   pFunctions.splice(myIndex,1)
  }
  public function removeAllFunctions(){
   pFunctions = new Array()
  }
  //
  //
  // *******************************************************************
  //
  //
  // DESTROY
  //
  //
  // *******************************************************************
  public function destroy(){
   removeAllFunctions()
   pFrameMonitor.removeEventListener(Event.ENTER_FRAME, doIdle)
  }
 }
}

Example Usage:

1
2
3
4
5
6
7
8
9
10
11
12
13
package{
  import flash.display.*
  public class Main{
    public var pBackground:Background 
    public function Main extends MovieClip(){
      pBackground = new Background(this, .2);
      myBackground.addFunction(myFunction);
    }
    public function myFunction(){
      // This function will get called until .2 of the ENTER_FRAME interval is used
    }
  }
}

You can add as many functions as you like to the pFunctions array, it will call each of them in the order they were added, and keep cycling until the allotted CPU time is used up.

You can download the class form here:
Background.as

Tags: , , , , , ,

No Comments

Introducing Rainbow Live

Code and Visual is happy to announce the launch of Rainbow Live XML Editor.

Rainbow has been developed to answer the needs of Flash developers who use XML as a data source.

With Rainbow Live you can combine the ease of Visual XML editing with the power of a Live Content Management System (CMS).

If you need to manage online XML data then Rainbow Live is for you. Rainbow Live makes it easy to design data in whatever structure you want, and then edit it online without having to re-upload new XML files.

Rainbow Live puts the power back into the hands of Flash developers. There’s no more need to rely on a backend developer to build and maintain a database. Rainbow Live saves time and money as the process of setting up XML structures is familiar to many Flash developers and is made even more intuitive with Rainbow Live’s node-based approach.

Rainbow Live provides you with a User log-in to allow data to be managed by the client long after your involvement is through. Rainbow Live provides the perfect lightweight solution when you don’t need all the bells and whistles (and overhead) of a complete CMS system.

While Rainbow Live is not technically a complete CMS, it provides all of the basic functionality needed to design, populate and edit data for small to medium based Flash projects (And any where else that XML is used as a data source).

Tags: , , , , , , , ,

No Comments