Ant Farm

Document Sample
Ant Farm Powered By Docstoc
					                                                        Ant Farm

Created with Macromedia Flash 2004 / Actionscript 2.0

//How many ants do we want?
        var numAnts:Number = 50;

         //Function for generating a random number
         var numRandom:Function= function(seed:Number) {
                   return Math.floor(Math.random()*seed);
         }

         //Function to create an ant
         var createAnt:Function = function () {
                   //Assign our new movieclip a name
                   var antName:String = "ant" + i;
                   //Attach the movie to the canvas
                   attachMovie("ant", antName, i);
                   //Generate a random size
                   var scaleFactor:Number = numRandom(9);
                   this[antName]._xscale = 8+scaleFactor;
                   this[antName]._yscale = 8+scaleFactor;
                   //Generate random speed, drift, coords, and angle
                   this[antName].speed = numRandom(3)+1;
                   this[antName].drift = numRandom(4)+2;
                   this[antName]._x = numRandom(500);
                   this[antName]._y = numRandom(500);
                   this[antName]._rotation = numRandom(360);
                   //Specify foot to step with first
                   var step:String = "right";
                   //Instantiate our rotation var
                   //so our ants can wander around aimlessly
                   var rotation:Number = 0;
                   //Create enterFrame event handler for animation
                   this[antName].onEnterFrame = function () {
                              //if we are stepping with our right foot
                              //and our legs are rotated less than 2 degrees
                              //rotate 1 degree
                              if ((this.antLegs._rotation < 2) && (step == "right")) {
                                          this.antLegs._rotation += 1;
                              }
                              //if we are stepping with our left foot
                              //and our legs are rotated less than -2 degrees
                              //rotate -1 degree
                              if ((this.antLegs._rotation > -2) && (step == "left")) {
                                          this.antLegs._rotation -= 1;
                              }
                              //If we are rotated all the way right
                              //switch to left
                              if (this.antLegs._rotation == 2) {
                                          step = "left";
                              }
                              //If we are rotated all the way left
                              //switch to right
                              if (this.antLegs._rotation == -2) {
                                          step = "right";
                              }
                                //Calculate the angle of the body
                                incRotation = (numRandom(100-(this.drift*rotation))-50)/100;
                                rotation += incRotation;
                                this._rotation += rotation;
                                //Calculate the position of the ant
                                posX =this.speed * Math.sin((this._rotation/180)*Math.PI);
                                posY = -this.speed * Math.cos((this._rotation/180)*Math.PI);
                                this._x += posX;
                                this._y += posY;
                    //If we are off the stage, send our ant back
                    //in the direction he came from
                    if ((((this._x < -20) ||
                                                   (this._x > Stage.width + 20)) ||
                                                   (this._y < -20)) ||
                                                   (this._y > Stage.height + 20)) {
                                this._rotation += 180;
                    }
         };
}

//Loop through the specified number of ants we
//want to create and create them
for (var i = 0;i < numAnts; i++) {
           createAnt();
}

				
DOCUMENT INFO
Shared By:
Categories:
Stats:
views:4
posted:4/28/2010
language:English
pages:2