forked from: circle bullet
// forked from shinh's circle bullet
package {
import flash.display.*;
import flash.text.*;
import flash.events.*;
public class FlashTest extends Sprite {
private var screen:BitmapData;
private const W:int = 465, H:int = 465;
private var cnt:int;
private var running:Boolean;
private var wall:Shape = new Shape();
private var balls:Array = new Array();
private var score:TextField;
private var text:TextField;
private function makeTextField(x:int, y:int, s:int):TextField {
var t:TextField = new TextField();
t.x = x;
t.y = y;
t.width = W;
t.height = H;
var fm:TextFormat = new TextFormat;
fm.color = 0xffffff;
fm.font = '_typewriter';
fm.size = s;
fm.bold = true;
t.defaultTextFormat = fm;
return t;
}
public function FlashTest() {
this.addEventListener(Event.ENTER_FRAME, update);
screen = new BitmapData(W, H, false, 0);
addChild(new Bitmap(screen));
wall.graphics.beginFill(0xffffff);
wall.graphics.drawRect(-4, -4, 9, 9);
addChild(wall);
addChild(score = makeTextField(10, 10, 20));
score.text = '0';
addChild(text = makeTextField(110, 200, 30));
stage.addEventListener(MouseEvent.CLICK, init);
running = true;
}
public function createBall(a:Number): void {
var ball:Shape = new Shape();
ball.graphics.beginFill(0xD22000);
ball.graphics.drawCircle(0, 0, 4);
addChild(ball);
ball.x = W / 2;
ball.y = H / 2;
var t:Number = Math.PI * cnt / 300;
var r:Number = Math.PI * 2 * Math.pow(Math.sin(t), 2);
balls.push(new Array(ball, Math.random() * 3 + 2,
a + Math.random() * r - r / 2));
}
public function init(event:MouseEvent): void {
if (running) return;
running = true;
score.text = '' + (cnt = 0);
text.text = '';
}
public function update(event:Event): void {
var a:Number = Math.atan2(stage.mouseX - W / 2,
stage.mouseY - H / 2);
wall.rotation = - a / 3.141592 * 180;
wall.x = W / 2 + Math.sin(a) * 200;
wall.y = H / 2 + Math.cos(a) * 200;
var nballs:Array = new Array();
for (var i:int = 0; i < balls.length; i++) {
var ball:Shape = balls[i][0];
var bx:Number = ball.x - W / 2;
var by:Number = ball.y - H / 2;
var da:Number = Math.abs(Math.atan2(bx, by) - a);
if ((da < 0.02 || da > Math.PI * 2 - 0.02) &&
Math.abs(Math.sqrt(bx*bx + by*by) - 200) < 4) {
text.text = ' GAME OVER\nCLICK TO START';
running = false;
}
ball.x += balls[i][1] * Math.sin(balls[i][2]);
ball.y += balls[i][1] * Math.cos(balls[i][2]);
if (ball.x > 0 && ball.x < W &&
ball.y > 0 && ball.y < H) {
nballs.push(balls[i]);
}
else {
removeChild(ball);
}
}
balls = nballs;
var n:Number = cnt * Math.random() / 300;
for (i = 0; i <= n; i++) {
if (running) createBall(a);
}
score.text = 'SCORE: ' + (cnt += running ? 1 : 0) +
'\nOBJS : ' + balls.length;
}
}
}