Return to Snippet

Revision: 9456
at November 7, 2008 11:49 by Winkyboy


Initial Code
// This script requires a movieclip in the library exported for actionscript as "particle_mc"

#include "mc_tween2.as" // from http://hosted.zeh.com.br/mctween/


var speed:Number = 1.2; // speed at which each particle rises per cycle
var lifespan:Number = 4000; // in milliseconds
var particleCount:Number = 30; // the system starts slowing down at/above 50.  This is just a quick & easy particle system, and AS2 isn't the best for particles, either.
var alphaInitial:Number = 80; // what alpha level each particle is set to upon spawning
var fadeRate:Number = .2; // how much per cycle each particle's alpha drops
var spawnMovieClip:MovieClip; // set this to a movieclip if you want to attach the steam to something
if(spawnMovieClip != undefined) {
	var spawnX:Number = spawnMovieClip._x;
	var spawnY:Number = spawnMovieClip._y;
} else {
	var spawnX:Number = Stage.width/2;
	var spawnY:Number = Stage.height/2;
}

var wandering:Array = Array(particleCount); // this is a per-particle left-right direction, so that the horizontal movement of each particle is less jumpy

var merging:Number = 0; // Don't change this.  An incrementing value.
var mergingLoop:Number = 1000; // length of merge cycle
var mergingThreshold:Number = 500; // change this to set the frequency of the tendency for particles to group together.  Higher is more frequent, Lower is less.













this.onEnterFrame = function() {
	for(intI = 0; intI < particleCount; intI++) {
		if(this["steam" + intI + "_mc"] == undefined) { // this happens once for each particle
			this.attachMovie("particle_mc", "steam" + intI + "_mc", this.getNextHighestDepth(), {_x:-500, _y:-500, _alpha:0});
			if(spawnMovieClip != undefined) this["steam" + intI + "_mc"].swapDepths(spawnMovieClip); // slips each particle underneath the attach object, if defined.
			var timeout:Number = setTimeout(spawnSteam, randRange(.25*lifespan,.75*lifespan), this["steam" + intI + "_mc"]); // "spawning" each particle really only resets its default properties, so that we can just recyle the movieclips
			wandering[intI] = 0;
		}


		merging++;
		if (merging == mergingLoop) merging = 0;


		wandering[intI] += randRange(-100,100)/100;
		
		if (merging < mergingThreshold) {
			var thisX:Number = this["steam" + intI + "_mc"]._x;
			var prevX:Number = this["steam" + (intI-1) + "_mc"]._x;
			var Dif:Number = thisX - prevX;

			if (Dif > 0) { // if we're currently to the right of the previous particle
				if (thisX - prevX > 10) { // if the difference is more than 10 pixels
					wandering[intI] = -1; // set the current particle's direction to move left
				}
			} else {
				if (prevX - thisX > 10) {
					wandering[intI] = 1;
				}
			}
		}


		// limit each particles horizontal movement to no more than one pixel per cycle
		if(wandering[intI] < -1) {wandering[intI] = -1}; 
		if(wandering[intI] > 1) {wandering[intI] = 1};
		

		// apply the changes to each particle
		this["steam" + intI + "_mc"]._x += wandering[intI];//randRange(-2*speed,2*speed);
		this["steam" + intI + "_mc"]._y -= speed;
		this["steam" + intI + "_mc"]._alpha -= fadeRate + randRange(2*fadeRate,4*fadeRate);
		this["steam" + intI + "_mc"]._rotation += (randRange(-150,150)/300);

	}
}

function spawnSteam (which_mc:MovieClip) {
	which_mc.scaleTo(randRange(20,50));
	which_mc.blurTo(10);
	which_mc._x = spawnX;// + 80 + randRange(20,40); // remove tweaking or change it if you want to adjust where in relation to the attach object you want each particle to spawn
	which_mc._y = spawnY;// + 140 + randRange(-5,5); // remove tweaking or change it if you want to adjust where in relation to the attach object you want each particle to spawn
	which_mc.alphaTo(alphaInitial, .25);
	var timeout:Number = setTimeout(spawnSteam, lifespan+randRange(-.5*lifespan,.5*lifespan), which_mc);
}

function randRange(min:Number, max:Number):Number {
    var randomNum:Number = Math.floor(Math.random() * (max - min + 1)) + min;
    return randomNum;
}


stop();

Initial URL
http://www.winkyboy.com/files/steam_template.zip

Initial Description
This is a CS3/AS2 Flash file containing a simple particle system.  It requires the use of MC Tween (http://hosted.zeh.com.br/mctween/).

Initial Title
AS2 Basic Steam Particles; requires MC Tween

Initial Tags
actionscript

Initial Language
ActionScript