AS3 Playground: Simple Particles

01/02/2010 in RIA View Comments

First off, I will say that without Matthew Tretter’s garbage collection classes this demo would not be easy…While generating this many “particles” on ENTER_FRAME, the Flash Player can not keep up (or wouldn’t in my case), with the 3 tweens on X, Y, and alpha.
After saying that…here is a demo that I came up with tonight playing around in Flash and practicing some rapid development prototyping. It is a “particle” generator (that’s what I am calling it) using the display class, randomly positioning X and Y, as well as tweening the alpha channel.
Keep in mind this is simply a playground exercise and not necessarily, or really at all, optimized for memory.

Particles
Go ahead, click and drag below…you know you want to.

import flash.display.MovieClip;
import fl.transitions.Tween;
import fl.transitions.easing.*;
import fl.transitions.TweenEvent;
import fl.transitions.*;
import fl.motion.easing.*;
 
//Matthew Tretter's Garbage collection class
import com.exanimo.transitions.GCSafeTween;
 
//VARIABLES
var circBubble:MovieClip;
var XLocMove:Number = 10;
var YLocMove:Number = 10;
var circRadius:Number = Math.random()*10;
var bubbleTween:Tween;
var bubbleColor:uint = 0x000000;
var bubbleArray:Array = new Array();
 
//Event Listeners
stage.addEventListener(MouseEvent.MOUSE_DOWN, checkMouseDown);
stage.addEventListener(MouseEvent.MOUSE_UP, checkMouseUp);
 
//Check Mouse Actions
function checkMouseDown(e:MouseEvent):void {
	stage.addEventListener(Event.ENTER_FRAME,tweenBubble);
}
function checkMouseUp(e:MouseEvent):void {
	stage.removeEventListener(Event.ENTER_FRAME,tweenBubble);
}
 
//Method to kick off the particles, gets called on every frame at 30fps.
function tweenBubble(e:Event):void {
 
	circBubble = new MovieClip();
	addChild(circBubble);
 
	circBubble.graphics.beginFill(bubbleColor);
	circBubble.graphics.drawCircle(mouseX,mouseY, circRadius);
	circBubble.graphics.endFill();
 
	//Matthew Tretter's garbage collection class calls the tweens
	new GCSafeTween(circBubble, "y", Elastic.easeOut, 0, XLocMove, 2, true);
	new GCSafeTween(circBubble, "x", Elastic.easeOut, 0, YLocMove, 2, true);
	new GCSafeTween(circBubble, 'alpha', None.easeNone, 1, 0, 1, true);
 
	//Randominze the XLOC - both negative and positive coordinate results
	if (XLocMove > -1) {
		XLocMove = (Math.random()*100) * -1;
	} else if (XLocMove < -1) {
		XLocMove = (Math.random()*100) + -1;
	}
	//Randominze the YLOC - both negative and positive coordinate results
	if (YLocMove > -1) {
		YLocMove = (Math.random()*100) * -1;
	} else if (YLocMove < -1) {
		YLocMove = (Math.random()*100) + -1;
	}
	//Randomize the radius of the generated bubbles
	circRadius = Math.random()*10;
 
	//Randomly add to the bubble Color, I am partial to blue...
	bubbleColor += 000022;
}

Other posts you may find helpful:

,

  • rv

    great example, nice job.

  • connatser

    thanks Richard, and thanks for checking it out!

blog comments powered by Disqus

Switch to our mobile site