doraを崩壊させてみた
@author minon@minomix.net
/**
* @author minon@minomix.net
*/
package {
import flash.display.*;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Rectangle;
import flash.text.*;
[SWF(backgroundColor="#000000",width="465",height="465",frameRate="60")]
public class BreakDora extends Sprite {
private var _canvas:BitmapData;
private var _source:BitmapData;
private var _t:TextField;
private var _dora:Dora;
private var _particles:Array = new Array();
private var _line:int = 0;
private var _n:int = 1;
public function BreakDora() {
_dora = new Dora(1);
_source = new BitmapData( _dora.width, _dora.height, true, 0x00FF0000 );
_source.draw( _dora );
this.addChild( new Bitmap( _source ) );
_canvas = new BitmapData( 465, 465, true, 0x00FF0000 );
this.addChild( new Bitmap( _canvas ) );
_t = new TextField();
_t.defaultTextFormat = new TextFormat( "_sans", 12, 0xFF0000 );
_t.text = "clickで崩壊";
_t.y = 200;
this.addChild( _t );
this.stage.addEventListener( MouseEvent.CLICK, clickHandler );
}
private function clickHandler(e:MouseEvent):void {
_line = _source.height;
this.addEventListener( Event.ENTER_FRAME, breakLine );
this.addEventListener( Event.ENTER_FRAME, update );
}
private function breakLine(e:Event):void {
var l:int;
for (var i:int = 0; i <= _n ; i++) {
for (var j:int = 0; j < _source.width ; j++) {
l = _line - i;
if ( _source.getPixel32( j, l ) != 0 ) {
emit( j, l, 1.05, _source.getPixel32( j, l ) );
_source.setPixel32( j, l, 0 );
}
}
}
_line -= _n;
if ( _line < 0 ) this.removeEventListener( Event.ENTER_FRAME, breakLine );
}
public function emit(ex:Number, ey:Number, s:Number = 1, c:int = 0xffffff ):void {
var p:Particle = new Particle();
p.x = ex;
p.y = ey;
p.xx = Math.random() * 2 + 1;
p.yy = Math.random() / 2;
p.s = s;
p.c = c;
_particles.push(p);
}
public function update(e:Event):void {
_canvas.lock();
_canvas.fillRect(this._canvas.rect, 0x00FF0000);
var n:int = _particles.length;
while (n--) {
var p:Particle = this._particles[n];
p.yy += 0.01 * p.s;
p.xx *= 0.999;
p.x += p.xx;
p.y += p.yy;
p.l--;
_canvas.fillRect( new Rectangle( p.x, p.y, 1, 1 ), p.c );
if ( p.y > stage.stageHeight || p.l < 0 || p.x < 0 || p.x > stage.stageWidth ) {
this._particles.splice(n, 1);
}
}
_canvas.unlock();
}
}
}
class Particle {
public var x:Number;
public var y:Number;
public var xx:Number;
public var yy:Number;
public var s:Number;
public var c:int;
public var l:int
public function Particle() {
this.x = 0;
this.y = 0;
this.xx = 0;
this.yy = 0;
this.s = 1;
this.c = 0x00FF00;
this.l = 50 + Math.random() * 50;
}
}
import flash.display.*;
import flash.geom.*;
class Dora extends Sprite{
public function Dora(num:int) {
var sp:Sprite = new Sprite();
addChild(sp);
var head:Sprite = new Sprite();
var w:Number = 94;
var h:Number = 82;
//頭を描く
head.graphics.beginFill(0xffffff * Math.random());
head.graphics.moveTo(-w/2, 0);
head.graphics.curveTo(-w/2+2, -h/2+1, 0, -h/2);
head.graphics.curveTo(w/2-2, -h/2+1, w/2, 0);
head.graphics.curveTo(w/2-1, h/2-1, 0, h/2);
head.graphics.curveTo(-w/2+1, h/2-1, -w/2, 0);
head.graphics.endFill();
sp.addChild(head);
//顔の輪郭を描く
var face:Sprite = new Sprite();
face.y = 7;
w = 86;
h = 67;
face.graphics.beginFill(0x000000);
face.graphics.moveTo(-w/2, 0);
face.graphics.curveTo(-w/2+2, -h/2+1, 0, -h/2);
face.graphics.curveTo(w/2-2, -h/2+1, w/2, 0);
face.graphics.curveTo(w/2-1, h/2-1, 0, h/2);
face.graphics.curveTo( -w/2+1, h/2 - 1, -w / 2, 0);
sp.addChild(face);
//目を描く
var lefteye:Sprite = new Sprite();
lefteye.x = -17.5;
lefteye.y = -35;
drawOval(lefteye, 17, 19);
sp.addChild(lefteye);
var eye1:Sprite = new Sprite();
eye1.graphics.beginFill(0x000000);
eye1.graphics.drawEllipse(0, 0, 8, 2);
eye1.x = -14;
eye1.y = -27;
sp.addChild(eye1);
var righteye:Sprite = new Sprite();
righteye.x = 0.5;
righteye.y = -35;
drawOval(righteye, 17, 19);
sp.addChild(righteye);
var eye2:Sprite = new Sprite();
eye2.graphics.beginFill(0x000000);
eye2.graphics.drawEllipse(0, 0, 8, 2);
eye2.x = 6;
eye2.y = -27;
sp.addChild(eye2);
//鼻を描く
var nose:Sprite = new Sprite();
nose.x = -5.5;
nose.y = -20;
drawOval(nose, 11, 9);
sp.addChild(nose);
//鼻下を描く
var noseline:Sprite = new Sprite();
noseline.graphics.lineStyle(1, 0xFFFFFF);
noseline.graphics.moveTo( -0.5, -18);
noseline.graphics.lineTo( -0.5, 24);
sp.addChild(noseline);
//口を描く
var mouse:Sprite = new Sprite();
mouse.graphics.lineStyle(1, 0xFFFFFF);
mouse.graphics.moveTo( -31, 6);
mouse.graphics.curveTo( -24, 24, 0, 24);
mouse.graphics.curveTo(24, 24, 31, 6);
sp.addChild(mouse);
//ヒゲを描く
var higeR:Sprite = new Sprite();
higeR.graphics.lineStyle(1, 0xFFFFFF);
higeR.graphics.moveTo( -16, -13);
higeR.graphics.lineTo( -35, -16);
higeR.graphics.moveTo( -17, -9);
higeR.graphics.lineTo( -36, -9);
higeR.graphics.moveTo( -16, -5);
higeR.graphics.lineTo( -35, -2);
sp.addChild(higeR);
var higeL:Sprite = new Sprite();
higeL.graphics.lineStyle(1, 0xFFFFFF);
higeL.graphics.moveTo( 16, -13);
higeL.graphics.lineTo( 35, -16);
higeL.graphics.moveTo( 17, -9);
higeL.graphics.lineTo( 36, -9);
higeL.graphics.moveTo( 16, -5);
higeL.graphics.lineTo( 35, -2);
sp.addChild(higeL);
sp.x += sp.width / 2;
sp.y += sp.height / 2;
}
private function drawOval(s:Sprite, w:Number, h:Number):void {
s.graphics.beginFill(0xFFFFFF, 100);
s.graphics.moveTo(0, h/2);
s.graphics.curveTo(0.5, 0.5, w/2, 0);
s.graphics.curveTo(w-0.5, 0.5, w, h/2);
s.graphics.curveTo(w-0.5, h-0.5, w/2, h);
s.graphics.curveTo(0.5, h-0.5, 0, h/2);
s.graphics.endFill();
}
}