In this experiment with actionscript 3.0,
I'll be playing around with 100.000 to 300.000 particles at a time.

Create particles is really easy in actionscript, and I'm always doing lots of experiments with lots of sprites or movieClips, and then move then around.

But the problem is that it doesn't take long to have so many particles on the stage that Flash can no longer handle it.
In fact, depending on the code you use for interacting btween this particles, it really doesn't take that many Sprites on the stage at all for Flash to start falling apart.

That is not good when you are trying to create some complex or nice effects.

So finally, after to much searching around the web, I found some nice ways to handle this problem, and some really nice results are comming out.

Online demo ( requires flash ).

The main problem we're dealing with here is how many objects are display on the stage at once.

Too many, and Flash goes crazy, so lets find a way to have not to much objects on the stage...
lets say just one.

So, lets just add one Bitmap object on the stage, and try to represent our particles there.

For now on , our particles will be virtual...
this will, of course, create some limitations, but for now lets create a Particle class who will store all information we need for our particle (speed, position, color, size, ... ).
Then on every frame we will run a loop over our particles, and draw them to our BitmapData.

Clear?

let's put some code down.

The first thing we need to do is create a Bitmap and BitmapData:

var bitmapData:BitmapData = new BitmapData( width, height, true, 0x00000000);
var bitmap:Bitmap = new Bitmap(bitmapData);
addChild(bitmap);

Now, we need to create our particles,

I was always storing my particles in a multidimensional array, but if we really need to move a lot of particles, better let's try to avoid multiple 'for' loops.

If you had work for example with C, you will remeber this:

obj = obj.next;

Lets bring back the 'next' property adding to our particle class a next pointer, allowing each particle, to have a pointer to the next particle.

_particles = new Particle();	// Pointer to the last particle
  
var currentParticle:Particle = _particles;
var numParticles:int = N_PARTICLES;

while( --numParticles >= 0 )
  {
  currentParticle.next = new Particle();
  currentParticle = currentParticle.next;
  }

With this, we had create N_PARTICLES, and each particle has a pointer that send you to the next one.
This means, that now, we can create an 'onEnterFrame' event and access our particles like this:

var particle:Particle = _particles;
while( particle )
  {
  // lets do something here with the particle
  particle = particle.next;
  }

Last part will be to draw our particles on the bitmap.

For this, lets back to C.
You will remeber that C hates multidimensional arrays, so lets supousse that flash, is not diferent, and even when an array is always easy to access and handle, we will use 1D vector to store our bitmap data.

"Vector in actionscript is just and array array whose elements all have the same data type."

_buffer:Vector.<uint>	= new Vector.<uint>( width * height, true );

first thing, will be reset the buffer to our backgorund color

nPixels = _buffer.length
while( --nPixels > -1 )
  buffer[ nPixel ] = colorBg;

now, lets draw in the buffer our particles

while( particle )
  {
  x = particle.x;
  y = particle.y;

  bufferIndex = x + int( y * bufferWidth );
  buffer[ bufferIndex ] = particle.color;
  }

and finally , draw our bitmap with the buffer data

_bitmap.lock();
_bitmap.setVector( _bitmap.rect, buffer );
_bitmap.unlock( _bitmap.rect );

Calling lock() on a BitmapData object forces ActionScript to not notify dependent Bitmap objects of the changes in our BitmapData, this will help us to improve our speed.

And that's in a big resume all,
you can see another example, moving just 150000 particles to create a galaxy.

Online demo ( requires flash ).

Another small detail that can help to improve our speed, is to set 'class final'.

In some situations, final method execute faster than non-final methods.
As we are looking to improve our application performance in every possible way, let's make our method final.

I'm happy with the amount of particles we can get running at once.
I still have to check the 'forEach' function in as3.0 and see if is faster than creating a next object, At last in Perl it works really good, but for now, the speed looks ok to do a lot of experiments.

By the way, the swfs are only around 8kb.


Download: ActionScript 3.0 Galaxy source files and demo