そして弾幕へ from: ぱーてぃくる
// forked from munegon's ぱーてぃくる
// write as3 code here..
package {
import flash.display.*;
import flash.events.*;
import flash.geom.*;
import flash.text.*;
public class BulletDemo extends Sprite {
private var SPEED_FO:Number = 0.8;
private var canvas_bd:BitmapData;
private var ctf:ColorTransform;
private var temp:Sprite;
private var tf:TextField;
public function BulletDemo () {
addEventListener( Event.ADDED_TO_STAGE, added );
}
private function added( e:Event ):void {
if ( e.target == this ) {
removeEventListener( e.type, arguments.callee );
stage.quality = StageQuality.LOW;
stage.frameRate = 60;
setup();
}
}
private function setup():void {
canvas_bd = new BitmapData( stage.stageWidth, stage.stageHeight, false, 0x000000 );
addChild( new Bitmap( canvas_bd ) );
ctf = new ColorTransform( SPEED_FO, SPEED_FO, SPEED_FO, 1.0 );
temp = new Sprite();
tf = new TextField();
tf.x = 5;
tf.y = 5;
tf.textColor = 0xffffff;
tf.mouseEnabled = false;
addChild( tf );
addEventListener( Event.ENTER_FRAME, update, false, 10 );
}
private function update( e:Event ):void {
for ( var i:int = 0; i < 3; ++i ) {
fire();
}
canvas_bd.unlock();
canvas_bd.lock();
canvas_bd.colorTransform( canvas_bd.rect, ctf );
tf.text = temp.numChildren.toString();
}
private function fire():void {
var x:Number = stage.stageWidth / 2;
var y:Number = stage.stageHeight / 2;
var v:Number = 1*Math.random();
var r:Number = 2*Math.PI * Math.random();
var a:Number = 0.05;
var vx:Number = v * Math.cos( r );
var vy:Number = v * Math.sin( r );
var vr:Number = 0.02;
var dir:Number = 1 - 2*Math.floor( 2*Math.random() );
var size:Number = 1 + 4*Math.random();
var color:uint = [0xff6666, 0x6666ff][(1+dir)/2];//0xffffff * Math.random();
var rect:Rectangle = new Rectangle( x, y, 2*size, 2*size );
var bullet:Sprite = new Sprite();
bullet.graphics.beginFill( color );
bullet.graphics.drawCircle( 0, 0, size );
bullet.graphics.endFill();
temp.addChild( bullet );
bullet.addEventListener( Event.ENTER_FRAME, function( e:Event ):void {
v += a;
r += vr * dir;
vx = v * Math.cos( r );
vy = v * Math.sin( r );
x += vx;
y += vy;
rect.x = x - size;
rect.y = y - size;
canvas_bd.fillRect( rect, color );
if ( canvas_bd.rect.contains( x, y ) == false ) {
bullet.removeEventListener( e.type, arguments.callee );
bullet.graphics.clear();
temp.removeChild( bullet );
bullet = null;
}
}, false, 1 );
}
}
}