フラゲ風…。
// write as3 code here..
package
{
import flash.display.*;
import flash.events.*;
import flash.text.*;
[SWF(backgroundColor="0x000000", frameRate="30")]
public class BulletTest extends Sprite
{
private const BULLET_NUM:uint = 200;
private static var bullets:Array;
private const ENEMY_NUM:uint = 5;
private var enemys:Array;
private var hitCnt:uint = 0;
private var tf:TextField;
public function BulletTest():void
{
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT
var i:uint;
// 弾配列
bullets = new Array(BULLET_NUM);
for (i=0; i<bullets.length; i++) {
bullets[i] = new Tokun();
bullets[i].visible = false;
addChild(bullets[i]);
}
// 母配列
enemys = new Array(ENEMY_NUM);
for (i=0; i<enemys.length; i++) {
enemys[i] = new Enemy();
enemys[i].visible = false;
addChild(enemys[i]);
}
//TF生成
tf = new TextField();
tf.textColor = 0xFFFFFF;
tf.selectable = false;
addChild(tf);
updateTextField();
addEventListener(Event.ENTER_FRAME, update);
}
/**
* onEnterFrame
*/
private function update(e:Event):void
{
var tmp:Tokun = getVoidChild(enemys);
if (tmp != null) {
var tx:Number = Math.random() * stage.stageWidth;
var ty:Number = Math.random() * stage.stageHeight;
tmp.visible = true;
tmp.setPt(tx, ty);
tmp.setVfromRad(Math.atan2(stage.mouseY - ty, stage.mouseX - tx), 10);
}
updateChild(bullets);
updateChild(enemys);
//あたり判定
var i:uint;
for (i=0; i<bullets.length; i++) {
if (bullets[i].visible == false) {
continue;
}
//マウス座標とあたり判定
if (bullets[i].hitTestPoint(stage.mouseX, stage.mouseY)) {
bullets[i].visible = false;
hitCnt++;
updateTextField();
}
}
}
/**
* 配列のオブジェクトにupdate()を実行
* @param ary update()対象
*/
private function updateChild(ary:Array):void
{
var i:uint;
for (i=0; i<ary.length; i++) {
if (ary[i].visible) {
ary[i].update();
}
}
}
/**
* 配列上で空いている(visible==false)オブジェクトを取得
* @param ary
* @return オブジェクトへの参照
*/
public static function getVoidChild(ary:Array):Tokun
{
var i:uint;
for (i=0; i<ary.length; i++) {
if (ary[i].visible == false) {
ary[i].visible = true;
return ary[i];
}
}
return null;
}
/**
* 弾を設置
* @param x
* @param y
* @param vx
* @param vy
*/
public static function setBullet(x:Number, y:Number, vx:Number, vy:Number):void
{
var tokun:Tokun = getVoidChild(bullets);
if (tokun != null) {
tokun.visible = true;
tokun.setPt(x, y);
tokun.setV(vx, vy);
}
}
/**
* テキストを更新
*/
private function updateTextField():void
{
tf.text = "HIT = " + hitCnt;
}
}
}
import flash.display.*;
class Tokun extends Sprite
{
protected var vx:Number;
protected var vy:Number;
public function Tokun():void
{
init();
vx = 0;
vy = 0;
}
public function init():void
{
graphics.clear();
graphics.beginFill(0x5BADFF);
graphics.drawCircle(0, 0, 3);
graphics.endFill();
cacheAsBitmap = true;
}
public function setV(vx:Number, vy:Number):void
{
this.vx = vx;
this.vy = vy;
}
public function setVfromRad(rad:Number, spd:Number):void
{
setV(
Math.cos(rad) * spd,
Math.sin(rad) * spd
);
}
public function setPt(x:Number, y:Number):void
{
this.x = x;
this.y = y;
}
public function update():void
{
this.x += vx;
this.y += vy;
if (isInScreen() == false) {
this.visible = false;
}
}
private function isInScreen():Boolean
{
if (this.x < -this.width) return false;
if (this.y < -this.height) return false;
if (this.x > this.stage.stageWidth) return false;
if (this.y > this.stage.stageHeight)return false;
return true;
}
}
class Enemy extends Tokun
{
private var frameCnt:uint = 0;
public override function init():void
{
graphics.clear();
graphics.beginFill(0xEA594F);
graphics.drawRect(-5, -5, 10, 10);
graphics.endFill();
cacheAsBitmap = true;
}
public override function update():void
{
if (frameCnt++ % 2 == 0) {
BulletTest.setBullet(x, y, vx/2, vy/2);
}
super.update();
}
}