Tutorials
Hungry AI - Actionscript 3 version
by admin on Jul.13, 2009, under Tutorials, flash widgets
This is an updated version of the original from the flash math creativity book. It was originally done by Keith Peters (http://www.bit-101.com). I’ve divided it into three separate classes to make it more expandable. Email me if you want the original source.
Main.as (Document Class)
package
{
import flash.display.MovieClip;
import flash.display.Shape;
import flash.events.Event;
import flash.geom.Point;public class Main extends MovieClip
{
private var skeetQuantity:int = 10
private var foodStartingPoint:Point = new Point(280,210)
private var food:Food;public function Main()
{
addTheFood()
createNewSkeets()
}private function addTheFood()
{
food = new Food()
food.x = foodStartingPoint.x
food.y = foodStartingPoint.y
}private function createNewSkeets()
{
for (var i:int = 0; i < skeetQuantity; i++)
{
var skeet:Skeet = new Skeet(food)
skeet.x = Math.random()*500+20;
skeet.y = Math.random()*360+20;
skeet.startEating()
addChild(skeet)
}
addChild(food)
}
}
}
Skeet.as
package
{
import flash.display.Shape;
import flash.events.Event;
import flash.geom.Point;public class Skeet extends Shape
{
private var capacity:int = 10
private var speed:int = 10
private var ate:Number = 0
private var full:Boolean = trueprivate var angle:Number;
private var xDistance:Number;
private var yDistance:Number;
private var restingPosition:Point = new Point(0, 0)private var food:Shape
public function Skeet(_food:Shape)
{
food = _food
drawSkeet()
}private function drawSkeet()
{
graphics.lineStyle(1,0xFFFFFF,1)
graphics.lineTo(0, 6)
graphics.drawEllipse( -3, 6, 6, 9);
}public function startEating()
{
addEventListener(Event.ENTER_FRAME, eat)
}private function eat(e:Event)
{
if (full == true)
{
xDistance = restingPosition.x - this.x;
yDistance = restingPosition.y - this.y;
ate -= .2;if (ate < 1)
{
full = false;
}
}
else
{
xDistance = food.x - this.x;
yDistance = food.y - this.y;
}angle = Math.atan2(yDistance, xDistance);
this.rotation = angle * 180 / Math.PI + 90;//trace("food and skeet hit " + "full " + full)
if (food.hitTestPoint(this.x, this.y, true) && !full)
{
food.scaleX -= .005;
food.scaleY -= .005;food.x += xDistance / 30;
food.y += yDistance / 30;ate++;
if (ate > capacity)
{
full = true;
restingPosition.x = Math.random() * 200 - 100 + this.x;
restingPosition.y = Math.random() * 200 - 100 + this.y;
}
}
else
{
this.x += this.xDistance / speed;
this.y += this.yDistance / speed;
}this.scaleX = 0.4 + ((ate * 10) / 100);
}
}
}
Food.as
package
{
import flash.display.Shape;
import flash.events.Event;
import flash.geom.Point;public class Food extends Shape
{
private var yTarget:Number = 210public function Food()
{
drawFood()
addListeners()
}private function drawFood()
{
graphics.beginFill(0×000,1)
graphics.lineStyle(2,0xFFFFFF,1)
graphics.drawCircle(0, 0, 35)
graphics.endFill()
}private function addListeners()
{
addEventListener(Event.ENTER_FRAME, checkFoodQuantity)
}private function checkFoodQuantity(e:Event)
{
if (this.scaleX < 0.1)
{
this.scaleX = this.scaleY = 1;
this.x = Math.random() * 500 + 20;
this.y = 400;
yTarget = Math.random() * 300;
}this.y += (yTarget - this.y) / 10;
}
}
}}