- Home
- Tutorials
- Flash
- Intermediate
- Object Orientation : Creating an ant swarm
Object Orientation : Creating an ant swarm

Part Three - Main Class
Milan Toth
Milan Toth is the Chief Flash Developer of Jasmin Media Group, he created one of the world's biggest flash media server system. He loves Eclipse and OS X, AS3 and JAVA, sci-fi and horror, metal and electronic.
package
{
import flash.geom.Point;
import flash.text.TextField;
import flash.events.Event;
import flash.display.Sprite;
import Settings;
import com.milgra.swarm.Ant;
import com.milgra.swarm.Food;
import com.milgra.swarm.Queen;
import com.milgra.swarm.Drone;
import com.milgra.swarm.Worker;
import com.milgra.swarm.IWorld;
import flash.events.MouseEvent;
// we implement IWorld interface, because no individual world class needed yet,
// all functions can be created in the main class
public class Swarm extends Sprite implements IWorld
{
public var fps:Number; // actual fps
public var stamp:Number; // timestamp for fps measurement
public var antList:Array; // active ants
public var foodList:Array; // active food
public var reset:Boolean; // have to wait one frameevent before reset because of thread asynchronity
public var scene:Sprite; // main display
public var fpsField:TextField; // textfield showing fps
public var countField:TextField;// textfield showing active objects
public function Swarm ( )
{
// set framerate
stage.frameRate = 30;
// wait until stage.stageWidth
addEventListener( Event.ENTER_FRAME , init );
}
public function init ( event:Event ):void
{
removeEventListener( Event.ENTER_FRAME , init );
// instance creation
scene = new Sprite( );
fpsField = new TextField( );
countField = new TextField( );
antList = [ ];
foodList = [ ];
stamp = ( new Date( ) ).time;
// adding displayobjects
addChild( scene );
addChild( fpsField );
addChild( countField );
// set up textfields
fpsField.textColor = 0xffffff;
countField.textColor = 0xffffff;
countField.y = 20;
// create starting configuration
for ( var a:int = 0 ; a < Settings.STARTING_FOODS ; a++ ) addFood( );
for ( var b:int = 0 ; b < Settings.STARTING_QUEENS ; b++ ) addQueen( Math.round( Math.random( ) * 0xffffff ) );
// step world on enterframe events
addEventListener( Event.ENTER_FRAME , stepWorld );
// reset and generate new instances on mouse click
stage.addEventListener( MouseEvent.MOUSE_DOWN , resetWorld );
}
// create new configuration
public function resetWorld ( event:MouseEvent ):void
{
// removing displayobjects
for ( var a:* in antList )
scene.removeChild( antList[a] );
for ( var b:* in foodList )
scene.removeChild( foodList[b] );
// reset arrays
antList = [ ];
foodList = [ ];
// generating random instance numbers
var food:Number = Math.round( Math.random() * Settings.STARTING_FOODS );
var queens:Number = Math.round( Math.random() * Settings.STARTING_QUEENS );
// creating configuration
for ( var c:int = 0 ; c < food ; c++ ) addFood( );
for ( var d:int = 0 ; d < queens ; d++ ) addQueen( Math.round( Math.random( ) * 0xffffff ) );
}
public function stepWorld ( event:Event ):void
{
// call all ants' step function
for ( var a:* in antList ) antList[a].step( );
// generating new food
if ( Math.random() < Settings.NEW_FOOD_POSSIBILITY ) addFood( );
// calculating fps
var now:Number = ( new Date( ) ).time;
var delay:Number = ( now - stamp ) / 1000;
fps = 1 / delay;
stamp = now;
// updating textfields
fpsField.text = String( Math.round( fps ) ) + " fps";
countField.text = String( antList.length + foodList.length + " objects");
}
public function addQueen ( xColor : uint ):void
{
// creating new queen at a random position
var queen : Queen = new Queen( randomPos( ) , xColor , this );
// adding queen to display list
scene.addChild( queen );
// pushing queen in antList
antList.push( queen );
}
public function addDrone ( xQueen:Queen ):void
{
var drone : Drone = new Drone( xQueen , this );
scene.addChild( drone );
antList.push( drone );
}
public function addWorker ( xQueen:Queen ):void
{
var worker : Worker = new Worker( xQueen , this );
scene.addChild( worker );
antList.push( worker );
}
public function removeAnt ( argAnt:Ant ):void
{
// remove ant from displaylist and from antList
for ( var a:* in antList )
if ( antList[a] == argAnt )
{
scene.removeChild( antList[a] );
antList.splice( a , 1 );
}
}
public function addFood ( ):void
{
// add new food
var food:Food = new Food( randomPos( ) , this );
scene.addChild( food );
foodList.push( food );
}
public function removeFood ( argFood:Food ):void
{
// remove food from displaylist and from foodList
for ( var a:* in foodList )
if ( foodList[a] == argFood )
{
scene.removeChild( foodList[a] );
foodList.splice( a , 1 );
}
}
public function getVisibleFood ( argAnt:Ant ):Food
{
// loop through every food
for ( var a:* in foodList )
{
// calculate distance
var distance : Number = Point.distance( foodList[a].position , argAnt.position );
// if food is in our sight return it
if ( distance < Settings.SIGHT_DISTANCE ) return foodList[a ];
}
return null;
}
public function getVisibleEnemy ( argAnt:Ant ):Ant
{
// loop through every ant
for ( var a:* in antList )
{
// if ant's color is not the same, it is enemy
if ( antList[a].color != argAnt.color )
{
// calcualte distance
var distance : Number = Point.distance( antList[a].position , argAnt.position );
// if enemy is in our sight
if ( distance < Settings.SIGHT_DISTANCE ) return antList[a];
}
}
// else return null
return null;
}
// generate random position
public function randomPos ( ):Point
{
var pos : Point = new Point( );
pos.x = Math.random( ) * stage.stageWidth;
pos.y = Math.random( ) * stage.stageHeight;
return pos;
}
}
}
And that's it. Check it under attachments. I've also attached the flex project source.
Spread The Word
1 Response to "Object Orientation : Creating an ant swarm" 
|
said this on 07 Jun 2007 6:46:49 PM CST
Fantastic tutorial. It de
I recom |


Author/Admin)