-
package {
-
import flash.display.Sprite;
-
-
[SWF(width=500, height=500, backgroundColor=0xEFEFEF, frameRate=30)]
-
public class Main extends Sprite{
-
private var _robotManager:RobotManager;
-
public function Main(){
-
_robotManager = new RobotManager(this, 12);
-
}
-
}
-
}
-
-
class RobotManager{
-
-
private var _robots:Array;
-
private var _top:Sprite;
-
private var _robotNum:int;
-
-
public function RobotManager(top:Sprite, robotNum:int){
-
_top = top;
-
_robotNum = robotNum;
-
_robots = new Array();
-
setupRobots();
-
top.addEventListener(Event.ENTER_FRAME, onRun);
-
}
-
-
private function setupRobots():void{
-
for (var i:int = 0; i<_robotNum; i++){
-
_robots[i] = new Robot();
-
_robots[i].x = 100+(i % 3) * 150;
-
_robots[i].y = 100+int(i / 3) * 100;
-
_robots[i].addEventListener("death", onRobotDie);
-
_top.addChild(_robots[i]);
-
}
-
}
-
private function onRobotDie(evt:Event):void{
-
for (var i:int = 0; i<_robotNum; i++){
-
if (_robots[i] == evt.currentTarget){
-
_robots.splice(i, 1);
-
}
-
}
-
_robotNum--;
-
}
-
-
private function onRun(evt:Event):void{
-
collisions();
-
}
-
-
private function collisions():void{
-
var currBot:Robot;
-
for (var i:int = 0; i<_robotNum; i++){
-
currBot = _robots[i];
-
for (var j:int = 0; j<_robotNum; j++){
-
if (currBot != _robots[j]){
-
var dx:Number = currBot.x - _robots[j].x;
-
var dy:Number = currBot.y - _robots[j].y;
-
if (Math.sqrt((dx * dx) + (dy * dy)) <40){
-
currBot.die();
-
}
-
}
-
}
-
}
-
}
-
}
-
-
import flash.display.*
-
import flash.events.*;
-
-
class Robot extends Sprite{
-
private var _inc:Number
-
private var _td:Number;
-
private var _theta:Number;
-
private var _rad:Number;
-
-
public function Robot(){
-
_theta = Math.random() * (Math.PI * 2);
-
_td = _theta;
-
_inc = Math.random() * .2 + .05;
-
_rad = _inc * 10;
-
with(graphics){
-
beginFill(0x666666);
-
drawRect(-10,-10,20,20);
-
endFill();
-
beginFill(0x333333);
-
drawRect(-5, -5, 20, 10);
-
lineStyle(0,0xAAAAAA);
-
endFill();
-
drawCircle(0,0,20);
-
}
-
addEventListener(Event.ENTER_FRAME, onRun);
-
}
-
-
private function onRun(evt:Event):void{
-
_td += _inc;
-
_theta += _inc * Math.cos(_td);
-
if (x <0 || y <0 || x> 500 || y> 500){
-
_theta += Math.PI;
-
}
-
x += _rad * Math.cos(_theta);
-
y += _rad * Math.sin(_theta);
-
rotation = _theta / Math.PI * 180;
-
}
-
-
public function die():void{
-
removeEventListener(Event.ENTER_FRAME, onRun);
-
addEventListener(Event.ENTER_FRAME, onDie);
-
}
-
-
private function onDie(evt:Event):void{
-
scaleX = scaleY += .1;
-
alpha -= .1;
-
if (scaleX> 2){
-
removeEventListener(Event.ENTER_FRAME, onDie);
-
if (parent){
-
parent.removeChild(this);
-
dispatchEvent(new Event("death"));
-
}
-
}
-
}
-
}
This code should be placed in a document class... not the timeline (hopefully you already know that)
The above creates 12 wandering robots that die when they collide with one another.
This is not a snippet really... but it's only slightly over 100 lines and it contains a few tricks I find myself using from time to time.
You can view the swf here.