Swarm: AS3 Experiment

May 29, 2011

So, what exactly is this? Well, it's a bunch of little black dots that, when all moving at once, imitate a swarm of insects. Yes, I was bored when I woke up this morning. But it can actually be pretty useful.

The Code

Firstly, make a new AS3 class file, and call it Bug.as. Open it, and paste in the following code:

package  {
	import flash.events.Event;
	import flash.display.MovieClip;
	public class Bug extends MovieClip {
		
		public var xspeed:Number = 0;
		public var yspeed:Number=0;
		public var range:Number=20;
		public var accel:Number=0.5;
		
		public function Bug() {
			this.addEventListener(Event.ENTER_FRAME, chasePoint);
		}
		
		public function chasePoint(e:Event):void {
			
			//Move the bug
			this.x += xspeed;
			this.y += yspeed;
			
			//Make it accelerate at a random speed
			accel=Math.random();
			
			//Go in the right direction
			if (this.x<stage.mouseX) {
				xspeed+=accel;
				
				//Slow down if it overshoots the target
				if (xspeed<0) xspeed+=1;
			}
			if (this.x>stage.mouseX) {
				xspeed-=accel;
				
				//Slow down if it overshoots the target
				if (xspeed>0) xspeed-=1;
			}
			if (this.y<stage.mouseY) {
				yspeed+=accel;
				
				//Slow down if it overshoots the target
				if (yspeed<0) yspeed+=1;
			}
			if (this.y>stage.mouseY) {
				yspeed-=accel;
				
				//Slow down if it overshoots the target
				if (yspeed>0) yspeed-=1;
			}
			
			//Make sure it's never still: If it gets too close, bounce around a bit.
			if (Math.round(this.x/2)==Math.round(stage.mouseX/2) || Math.round(this.y/2)==Math.round(stage.mouseY/2)) {
				xspeed+=Math.random()*range-range/2;
				yspeed+=Math.random()*range-range/2;
			}
		}

	}
	
}

Now, in a new AS3 Flash file, create an image of a bug, and convert it to a MovieClip. When doing so, click the "advanced options" button, and check the "Export for Actionscript" box. Set the class name to Bug (the same name as the class file, you see.)

Then, in the first frame of the main timeline, add the following actions:

var init_x:Number=200;
var init_y:Number=200;
var range:Number=100;

for (var i=0; i<120; i++) {
	var bug1:Bug = new Bug();
	bug1.x = (Math.random() * range)+init_x-range/2;
	bug1.y = (Math.random() * range)+init_y-range/2;
	stage.addChild(bug1);
}

How it Works

Basically, it creates a bunch of bugs in a for loop. You can change the number of bugs created by changing this line:

for (var i=0; i<120; i++) {

...to something like this:

for (var i=0; i<50; i++) {

Notice the difference in the i<number part. That's how many bugs are made.

What point do they follow? Well, you could easily go through and change stage.mouseX and stage.mouseY to something like character.x and character.y. That way, it can follow any movieclip on your screen.

But what if the thing you want to follow, perhaps a character in a frame-by-framed animation, is not a movieclip? Simply make a circle with Alpha=0 (so it's invisible), make it a movieclip, and make it follow the character. To the audience, it will simply look like the bugs are following the character directly.

 

Demo

Here's what it looks like used alongside an animation:

Hopefully you'll be able to use this for something!

</david>