Creating simple particles with ActionScript 3.0

November 3rd, 2009 by Alex Leave a reply »

This time we’ll make an introduction to creating particles with Flash and ActionScript 3.0. You’ll need some Flash UI experience in order to complete the task.
Let’s start from creating a new flash 9 document, and creating the first particle movie clip.
[Step 1]

Create some simple rotating and fading animation using the flash UI. Then, at the end of the animation type:

r.removeChild(this)

This will remove the movie clip from stage after playing the animation. We’ll set the r variable a bit later.

Next step is to link the movie clip to a proper class. Right-click the created movie clip in the Library and click Linkage to set the class name.
[Step 2]

Now, let’s create a file named particle.as and describe our class:

package {
 import flash.display.MovieClip;
 
 public class particle extends MovieClip {
  public var r:MovieClip;
 
  public function particle() {
  }
 }
}

This is just a simple class, containing only one variable – we will use it to link to our main root movie.
Now, on main stage frame, add the enterFrame event, and define a callback function. This function will contain the simple x and y particle setting, and just some basic color transformation:

addEventListener('enterFrame', tick);
stage.scaleMode = 'noScale';
 
function tick(e:Event) {
	var m = new particle();
	m.x = mouseX;
	m.y = mouseY;
	var c = new ColorTransform(0,0,0,1,Math.random()*255, Math.random()*255, Math.random()*255);
	m.transform.colorTransform = c;
	m.r = this;
	this.addChild(m);
}

This is it. You can download the source files to check how it’s actually done.
creating-simple-particles-with-actionscript-3.0.zip




Comments are closed.