H3XED

ActionScript 3 Object Pool Tutorial

Jul 17, 2012   Programming   Nick Vogt   Comments
Please note that this post is over a year old and may contain outdated information.
ActionScript 3If you're unfamiliar with object pooling, it is a way to speed up processing time by keeping objects in memory and reusing them, instead of always instantiating new ones and garbage collecting them.

This example uses package and custom class syntax. If you are unfamiliar with these, see my introduction to package and classes post.

Below is an example ParticleHandler class. This class would handle the particles in a game, moving them, and removing them when they're used up. It also has a public create method, which allows any class to instruct this class to create new particles. The pool starts out empty and is filled up as needed.

The example is commented so that you should be able to understand it. The main Array is "particleList" that contains the currently active particles, and the pool is "particlePool" that contains currently unused particles waiting to be used. In my games I use typed Vectors and a custom class for the Particles to speed things up, but in this example it is easiest to just use basic Arrays and Objects for demonstration.

package
{
   public class ParticleHandler
   {
      private var i:int; // Used in below loop
      private var particle:Object; // Reference used below

      // Keep track of count since it's faster than accessing length property
      private var listCount:int = 0;
      private var poolCount:int = 0;
      
      private var particleList:Array = new Array();
      private var particlePool:Array = new Array();
      
      // Your enter frame event handler - probably driven by the EnterFrame Event
      // This performs the particle logic, such as moving them around, decrementing life, etc
      public function frameHandler():void
      {
         // Step backwards through list in case we remove during loop
         for(i = listCount - 1; i >= 0; i --)
         {
            // Assign reference to current particle for ease of access
            particle = particleList[i];
            
            // Particle logic
            particle.x+= particle.vX;
            particle.y+= particle.vY;
            particle.life --;
            
            // When particle is used up, remove from list and put in pool
            if(particle.life <= 0)
            {
               // Remove from list
               particleList.splice(i, 1);
               listCount --;
               
               // Add to pool (inserting using the count is faster than using push method)
               particlePool[poolCount] = particle;
               poolCount ++;
            }
         }
      }
      
      // Create particle
      public function create(X:Number, Y:Number, Life:int = 100):void
      {
         // Pull from pool if available
         if(poolCount > 0)
         {
            // pop removes last entry and returns it - fastest way to remove from an array
            particle = particlePool.pop();
            poolCount --;
         }
         
         // Otherwise create a new one
         else
         {
            particle = new Object();
         }
         
         // Assign attributes to the particle
         particle.x = X;
         particle.y = Y;
         particle.vX = Math.random() - 0.5; // Random movement just for example
         particle.vY = Math.random() - 0.5;
         particle.life = Life;
         
         // Insert into main list array
         particleList[listCount] = particle;
         listCount ++;
      }
   }
}
Share This Post
Twitter

Comments (0)

Share This Post
Twitter
H3XED © Nick Vogt   RSS   Policies   Twitter