How to make particle explosions

10/11/2007

As a bit of a treat, I’m going to show you how to make particle “explosions” that will come in handy in games, visulisations or other projects.

Here is what we’ll be making: (click to create an explosion.)

(Either JavaScript is not active or you are using an old version of Adobe Flash Player. Please install the newest Flash Player.)

Download .zip source files here (6.83KB)

First of all, create a new Flash document. The width and height you choose doesn’t really matter.


Press ctrl+F8 to create a new Movie Clip. Enter Particle for the name.

What we want to do here is go to the advanced options and tick the box that says Export for ActionScript. This will mean that we can link the graphics of this movie clip to the class that we will create later on.

In the textbox labeled Class, enter Particle.

Particle explosion1

What the create MovieClip window should look like

After entering these settings, press ok. Create a square that is around 3px wide and 3px height. The colour doesn’t matter as we will change this dynamically later on.

Go back to the main timeline.

Now we have our Particle MovieClip set up. At the moment, this doesn’t do anything at all.

Let’s add some ActionScript.

Press File -> New and create a new ActionScript File (*.as)

Particle Explosion 2

Save this file as Particle.as

Make sure that when you save this file, it is in the same folder as your main .fla file.

The ActionScript that we will put into this Particle.as file will control each particle that is on the screen.

class Particle extends MovieClip{
var vx,vy;
var timeToLast,startTimer;
var myColor,rgbValue;
	function onLoad(){
		var angle = Math.random()*360;
		vx = Math.cos(angle)*Math.random()*20+5;
		vy = Math.sin(angle)*Math.random()*20+5;
		timeToLast = Math.random()*500 + 1000;
		startTimer = getTimer();
		myColor = new Color(this);
		myColor.setRGB(rgbValue);
	}
	function onEnterFrame(){
		this._x += vx;
		this._y += vy;
		this._alpha -=2;
		if(getTimer() - startTimer >= timeToLast || this._alpha == 0){
			removeMovieClip(this);
		}
	}
}

It may look confusing, but I’ll take you through it and explain it.

class Particle extends MovieClip{

This just sets Particle up as a class that extends the MovieClip class. This means that the class Particle will inherit features from the MovieClip class.

var vx,vy;
var timeToLast,startTimer;
var myColor,rgbValue;

Here we’re just setting up variables for our Particle. vx, vy (x and y velocities), timeTolast,startTimer (how long the particle lasts and what time the particle is first created at), myColor,rgbValue (for setting our Particle’s colour.)

function onLoad(){
	var angle = Math.random()*360;
	vx = Math.cos(angle)*Math.random()*20+5;
	vy = Math.sin(angle)*Math.random()*20+5;
	timeToLast = Math.random()*500 + 1000;
	startTimer = getTimer();
	myColor = new Color(this);
	myColor.setRGB(rgbValue);
}

When our particle loads we set up the variables for it. Here we are setting it’s x and y velocities and the time it can last on stage.

myColor = new Color(this);
myColor.setRGB(rgbValue);

This is how we change our Particle’s colour. We create a Color object, myColor and assign it to this particle. We then proceed to give it a rgbValue (which will be passed to the particle later on.)

function onEnterFrame(){
	this._x += vx;
	this._y += vy;
	this._alpha -=2;
	if(getTimer() - startTimer >= timeToLast || this._alpha == 0){
		removeMovieClip(this);
	}
}

This is where we decide what the particle will do. What this particle is doing at the moment is just moving in a random direction. Its opacity is lowered and if it’s _alpha reaches 0 (or the particle has been on stage for long enough), the particle is removed to free up the cpu.

This is all we need to manage each particle. Creating the particles is easy. In the main . fla file, enter this code in the timeline (on frame 1.)

var currentParticle = 0;
var colors = new Array("0xFF0000","0x00FF00","0x0000FF","0xFFFF00","0xFF00FF","0x00FFFF");

onEnterFrame = function(){
}

onMouseDown = function(){
	createExplosion(_xmouse,_ymouse,100,Math.round(Math.random()));
}

function createExplosion(x,y,max,sameColor){
	var c;
	if(sameColor){
		c = colors[int(Math.random()*colors.length)];
	}
	for(var i=0; i < max; i++){
		if(!sameColor){
			c = colors[int(Math.random()*colors.length)];
		}
		attachMovie("Particle","P"+currentParticle,getNextHighestDepth(),{_x:x,_y:y,rgbValue:c});
		currentParticle++;
	}

}

The only slightly confusing part of this code is this line:

attachMovie("Particle","P"+currentParticle,getNextHighestDepth(),{_x:x,_y:y,rgbValue:c});

All this does is attaches the MovieClip Particle (which is linked to our Particle.as script) to the movie. It is given the name “P”+currentparticle and is placed at the next highest depth. The last part of this line is where we pass some values to the object (x,y and a colour.)

Thank you for reading and I hope that you have learnt something.

Links: A very nice AS3 particle explosion effect here

There are 21 comments in this article:

  1. 13/11/2007very asteroids! say:

    great effect…i’m gonna have to make an asteroids game now!…of course, somebody probably already beat me to it… ;) …oh well…

  2. 7/12/2007Joe say:

    I’m using Flash MX and I’ve done everything in this tutorial that needed be. I get an error that says unexpected gt? Could you possibly help me solve this?

  3. 7/12/2007Andrew say:

    Sorry about that Joe, it seems WordPress turned some of my > signs into &gt when I edited this post.

    It’s fixed now, sorry for the inconvenience!

  4. 8/12/2007Paraltere say:

    hm.. thank you

  5. 10/12/2007steve Ellis say:

    hi
    have tried to create this effect. but an getting this error.

    **Error** /Users/apple/Desktop/ParticleExplosions/particle.as: Line 1: The class ‘Particle’ needs to be defined in a file whose relative path is ‘Particle.as’.
    class Particle extends MovieClip{

    Total ActionScript Errors: 1 Reported Errors: 1

    any ideas

    regards

    steve

  6. 10/12/2007Andrew say:

    Hi Steve, it sounds like you have Particle.as saved in a different folder than your main .fla file. Either that or you may have accidentally misspelled Particle.as

    Check the filenames and their locations and tell me if it doesn’t work :)

  7. 11/12/2007stani say:

    hey guys,
    i’m using mx2004. I get the error message like o/p 1.Anyone can help me plz..?

  8. 11/12/2007Andrew say:

    Hey Stani, can you elaborate a bit on the error message that you receive? Nothing comes to mind with just o/p.

    If you do that, I’ll see what I can do to help.

  9. 10/01/2008Matt say:

    Trying to get this to work on the root “onEnterFrame” action. I’d like to see it explode 1 or 2 times then stop. In actions I have :
    “stop();”
    and
    “onEnterFrame = function(){
    createExplosion(Stage.width/2,Stage.height/2,10,Math.round(Math.random()));
    }”
    But it just keeps making explosions over and over again. Any suggestions on how to fix that?

  10. 11/01/2008Matt say:

    Nvm. I figured it out. I nested the whole thing in a movieclip and then controlled it from the root with intervals.

  11. 11/01/2008Andrew say:

    Good thinking. I’m glad you figured it out :)

  12. 22/01/2008mew say:

    i dont understand a bit but im not suprised cos i dont understand actionscript yet

  13. 22/01/2008Rob say:

    Hey, everytime i try to run it I get a syntax error saying that I need a semi colon somewhere in the first line. Oh and Im using Flash Mx the newest one. Can anyone help?

  14. 22/01/2008Andrew say:

    @Rob
    Hi Rob, I’ve searched the code and I can’t find any syntax errors. The only thing I can suggest is recopying the code and try again, since I can’t seem to find anything.

    Let me know if you still have troubles and I’ll have another look!

  15. 17/02/2008Lucha say:

    This is a really cool animation.
    i was just wondering if i wanted to add a trailing effect to the particles to make it look like fireworks…how about i about doing that?

  16. 20/05/2008Hasiram say:

    Hello!

    Do you know how do i made that effect with words?

    With the interaction of the mouse the words explode!?!

    Thanks!

  17. 20/05/2008Tyler say:

    Hello, i downloaded the finshed files but i cant figure out how to add a custom cursor. the main.fla says its unexpected file and it wont open. So can you help me add a cursor. thanks.

  18. 25/06/2008nessa say:

    this error came up please help

    **Error** Symbol=particle, layer=Layer 1, frame=1:Line 1: The class or interface ‘particle’ could not be loaded.

  19. 18/08/2008Flash Particle Effects Tutorials Part 2 | Lemlinh.com say:

    […] Read more Share and Enjoy: […]

  20. 6/09/2008Marion say:

    I love this tutorial. It really works but mine only has one color (black) for the particles. How can I get different colors. I followed everything exact!

  21. 20/09/2008kamal say:

    great effect…I love this tutorial

Write a comment: