flash on 2010-1-18
package {
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
public class FlashTest extends Sprite {
public var Bullets:Array;
public var Enemies:Array;
public var ship:Sprite;
public function FlashTest(){
Bullets = new Array();
Enemies = new Array();
ship = new Sprite();
addChild(ship);
with(ship.graphics){
beginFill(0xff0000);
moveTo(0,-20);
lineTo(10,0);
lineTo(-10,0);
lineTo(0,-20);
endFill();
}
ship.x = stage.stageWidth/2;
ship.y = stage.stageHeight;
stage.addEventListener(Event.ENTER_FRAME,main);
stage.addEventListener(MouseEvent.MOUSE_DOWN,mousedown);
}
public function main(e:Event):void{
//addEnemy();
//addBullet();
ship.x = mouseX;
for(var i:int=0;i<Bullets.length;i++){
var b:Sprite = Bullets[i];
b.y -= 10;
if(b.y < -10){
removeChild(b);
Bullets.splice(i,1);
i -= 1;
}
}
for(i=0;i<Enemies.length;i++){
b = Enemies[i];
b.y += 3;
if(b.y > stage.stageHeight+20){
removeChild(b);
Enemies.splice(i,1);
i -= 1;
}else if(b.alpha == 0.5){
for(var j:int=0;j<Enemies.length;j++){
if(i != j && Math.sqrt(Math.pow(Enemies[j].x-b.x,2)+Math.pow(Enemies[j].y-b.y,2)) < 40){
Enemies[j].alpha = 0.5;
with(Enemies[j].graphics){
clear();
lineStyle(1,0x000000,1);
beginFill(0x0000ff);
drawCircle(0,0,20);
endFill();
}
}
}
removeChild(b);
Enemies.splice(i,1);
i -= 1;
}else{
for(j=0;j<Bullets.length;j++){
if(b.hitTestPoint(Bullets[j].x,Bullets[j].y,true)){
//removeChild(Bullets[j]);
//Bullets.splice(j,1);
//j = Bullets.length;
//Enemies.splice(i,1);
//removeChild(b);
//i -= 1;
b.alpha = 0.5;
with(b.graphics){
clear();
lineStyle(1,0x000000,1);
beginFill(0x0000ff);
drawCircle(0,0,20);
endFill();
}
}
}
}
}
}
public function mousedown(e:MouseEvent):void{
addEnemy();
addEnemy();
addBullet();
}
public function addBullet():void{
var b:Sprite = new Sprite();
addChildAt(b,0);
with(b.graphics){
lineStyle(1,0x000000,1);
beginFill(0xffffff);
drawRect(-3,0,6,20);
endFill();
}
b.x = ship.x;
b.y = ship.y;
Bullets.push(b);
}
public function addEnemy():void{
var e:Sprite = new Sprite();
addChildAt(e,0);
with(e.graphics){
lineStyle(1,0x000000,1);
beginFill(0xffffff);
drawCircle(0,0,20);
endFill();
}
e.x = Math.round(Math.random()*stage.stageWidth);
e.y = -20;
Enemies.push(e);
}
}
}