- Home
- Tutorials
- Flash
- Intermediate
- A simple particle system using Actionscript 3
A simple particle system using Actionscript 3

AS3 Particle System
Sam Coles
A Flash developer from Wisconsin happily tapping ActionScript away for the past few years.
View all articles by Sam ColesAnd it's true, Actionscript 3 is what a lot of developers had hoped for with version 2 and so much more. Movieclips and Actionscript can finally work in true harmony and there's no better way to show this relationship is by making a simple particle system.
Our system will consist of three things: the environment, a generic particle, and a specific particle. The environment will be the flash file itself where we make the rules on how the particles behave: where they're created and how they're created. The generic particle will contain the basic framework for our specific particle: a trajectory and position that's updated by variables supplied via the environment. The specific particle will actually be what we're generating and because it has unique aspects is best as a subclass of our generic particle.
Lets start out by creating the base of our system: the generic particle. Without this the rest of our system couldn't be created. Starting out by first thinking what the generic particle needs. In Actionscipt 3 you have to import most things so it's best to think ahead. Lets see, we need a way to update the particle, preferably time based, and it needs to be able to be displayed as well, it also needs to store x,y coordinate data. Thankfully there are classes just for that: Timer, TimerEvent, Point, and MovieClip.
package{
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.geom.Point;
import flash.display.MovieClip;
Simple enough, now for the tough part. What input from the environment does our particle need? For our purposes we are going to be thinking simple physics. That means four things: a trajectory, a stored position, a constant for gravity, and friction. We also need to store these values specific to the particle.
public class Particle extends MovieClip{
protected var position:Point;
protected var vector:Point;
private var gravity:int;
private var friction:Number;
public function Particle($position:Point, $vector:Point, $gravity:int, $friction:Number){
position = $position;
vector = $vector;
gravity = $gravity;
friction = $friction;
With the basic values added to our particle it's now time to update their values via a timer. Timers are the new setInterval; I prefer using Timers over frame based events myself. It's quite straight forward. First a timer needs to be created that will trigger at a set interval. Then the particle needs to listen to that timer for that trigger and call a method once it happens. First we have to add a class variable for our timer.
protected var update_i:Timer;
Then we can create a timer and add a listener in the constructor that's currently open to finish it off.
update_i = new Timer(25); //25ms for smooth movement
update_i.addEventListener(TimerEvent.TIMER, update, false, 0, true);
update_i.start();
}
The event listener is going to want to trigger a public method called update so we better get onto creating that. When writing handlers in Actionscript 3 they must receive an event object as their first parameter. Now, when our particle moves three things happen: the position gets updated via the movement vector, the y portion of the vector gets affected by gravity, and the x portion of the vector gets affected by the friction.
public function update($evt:TimerEvent):void{
//Apply The Vector To The Position
position.x += vector.x;
position.y += vector.y;
//Apply gravity
vector.y += gravity;
//Apply Friction!
vector.x *= friction;
}
Great now the generic particle class is done, time to close the class and package and move onto the specific particle.
}
}
For the specific particle we need to create a graphic, for all intensive purposes a ball will suffice. Create a new movieclip in the library and draw a circle in it. Once you have drawn the movieclip, right click on it in the library and choose linkage. You'll notice that the linkage window is now different and instead of an identifier it asks for a class. Input "Ball" into the class textbox and press the OK button. Ignore any warning flash gives you, we'll write a class soon enough.
The Ball class will be similar to the particle class (it is a subclass after all). Remember, the Particle class still needs all the parameters from the environment so the Ball class has to pass them on.
package{
import flash.events.TimerEvent;
import flash.geom.Point;
public class Ball extends Particle{
public function Ball($position:Point, $vector:Point, $gravity:int, $friction:Number){
super($position, $vector, $gravity, $friction); //Pass the torch
We have to also set the initial position of the Ball clip.
//Set Initial Position
x = position.x;
y = position.y;
As you could see in the Particle class we changed the position variable but never actually modified the x and y value of the clip. This is what the Ball class will be responsible for. To archive this all that's needed is to listen, again, for a timer event and call a method. Then, we're done!
update_i.addEventListener(TimerEvent.TIMER, setPosition, false, 0, true);
}[as]
public function setPosition($evt:TimerEvent):void{
x = position.x;
y = position.y;
}
}
}
Now it's finally some time for a little fun. Lets get these babies moving. The environment will be, what else, our main stage. On the first frame to be exact. First things first, a timer and a listener.
var ballTimer:Timer = new Timer(25);
ballTimer.addEventListener(TimerEvent.TIMER, throwBall, false, 0, true);
ballTimer.start();
Next we set the environment variables for our balls.
//Environment Variables
var gravity:int = 1.5;
var friction:Number = .85;
For the throwBall function we can have a little fun and use the mouseX and mouseY properties and some Math.random to make a fountain type effect.
function throwBall($evt:TimerEvent):void{
var tBall:Ball = new Ball(new Point(mouseX, mouseY), new Point(
(Math.random()-Math.random())*5, -Math.random()*8), gravity, friction);
addChild(tBall);
}
Test your movie and there you have it, some particles. Not the most impressive thing in the world but the concepts we covered (whether you knew it or not) are important. Separate what is different and keep it separate. We had three different pieces to this puzzle, the Particle class, the Ball class, and the environment. By keeping each of these separate we can easily change and modify the behavior or look of the particles we're creating. With some tweaking you can easily create yourself a nice little sparkler like this.
Spread The Word
Related Articles
12 Responses to "A simple particle system using Actionscript 3" 
|
said this on 17 May 2007 1:26:53 PM CDT
Hi, your tutorial was to be my first foray into the strange and alien world of AS3. I have coded in AS2 years now and can see applications in AS2 like Neo sees the Matrix. Sort of.
Anyway, I attempted to follow your instructions, but every time I attempted to test and compile, the following errors were given: 1. [as] should not be there. 2. The class 'Ball' must subclass 'flash.display.MovieClip' 3. The protected attribute can only be used in class property definitions. I have copied your code verbatim into 'Particle.as', 'Ball.as' and frame 1 of 'main.fla'. Where am I going wrong? |
|
said this on 21 May 2007 8:12:57 AM CDT
1. That's an error in the article that was made upon publishing.
2. Odd, since it extends Particle and particle extends flash.display.MovieClip it should work fine. Are you using Flash CS3's compiler or the flex2 commandline compiler? 3. Put the property by all the rest up at the top of the class. The tutorial isn't sequential in this case, sorry bout the confusion. -Sam |
|
said this on 21 May 2007 5:09:18 PM CDT
You should remove the [as] from your code, it's a Typo from placing the code in the post.
And it sounds like you are importing the Particle class somwhere… Have you defined it as the document class on accident? If you omit the [as] and make sure all your files (the fla, Particle, and Ball class) are in the same folder you should be good to go, it worked for me. |
|
said this on 24 May 2007 1:15:46 PM CDT
Is there a source file for this. I am intermediate at best in fla. I got lost trying to figure out where the first half of the .as is to be stored for import in the first frame.
Thank You |
|
said this on 26 May 2007 2:58:54 PM CDT
I'm also having an error compiling. I have three files: test.fla, particle.as and ball.as - all are saved in the same directory, but when I compile test.fla I get this error:
1136: Incorrect number of arguments. Expected 0. |
|
said this on 29 May 2007 6:56:54 AM CDT
partcle coding is great
|
|
said this on 07 Aug 2007 9:00:17 AM CDT
Hi i'm new to AS3, can you tell me what does the $ sign means before the argument?
public function Ball($position:Point, $vector:Point, $gravity:int, $friction:Number) nice tutorial regards |
|
said this on 24 Oct 2007 1:30:35 PM CDT
ArgumentError: Error #1063: Argument count mismatch on Particle$iinit(). Expected 4, got 0.
|
|
said this on 24 Oct 2007 2:38:27 PM CDT
I get the following error any light on this ???
ArgumentError: Error #1063: Argument count mismatch on Particle$iinit(). Expected 4, got 0. |
|
said this on 28 Oct 2007 7:58:04 AM CDT
It's no wonder everyone is having trouble with this - its badly written and pieces are given in the wrong order.
After a bit of fooling though, it works. |
|
said this on 21 Nov 2007 8:44:27 PM CDT
It is a bit confusing with the out-of-sequence code snippets and vague instructions on where to actually paste the code snippets to, but unlike many "tutorials" it makes you think about what you're doing instead of just blindly cut-and-pasting and running the finished app.
|
|
said this on 10 Dec 2007 10:05:02 PM CDT
This is very cool. Will you mind if I demonstrated this tutorial in a video format? I think a little "show-me" might help others a bit.
|



Author/Admin)