forked from: fla01
最初の埋まっていく感じが好きなので、クリックですべてをrandomizeするように
// forked from borealkiss's fla01
// 最初の埋まっていく感じが好きなので、クリックですべてをrandomizeするように
package {
import __AS3__.vec.Vector;
import flash.display.Sprite;
import flash.events.MouseEvent;
[SWF(width="400",height="400",backgroundColor="0x0",frameRate="24")]
public class Fl028_Main extends Sprite{
private const STAGE_WIDTH:Number = 400;
private const STAGE_HEIGHT:Number = 400;
private const PADDING_X:Number = 10;
private const PADDING_Y:Number = 10;
private const SPAN_X:Number = 4;
private const SPAN_Y:Number = 4;
private const LIMIT_X:uint = 20;
private const LIMIT_Y:uint = 20;
private var _canvas:Sprite;
private var _rects:Vector.<RandomRectFill> = new Vector.<RandomRectFill>();
public function Fl028_Main(){
_canvas = new Sprite();
this.addChild(_canvas);
this.draw();
this.stage.addEventListener(MouseEvent.CLICK,onClick);
}
private function draw():void{
var w:Number = (STAGE_WIDTH - 2*PADDING_X)/LIMIT_X - SPAN_X;
var h:Number = (STAGE_HEIGHT - 2*PADDING_Y)/LIMIT_Y - SPAN_Y;
for (var i:int=0; i<LIMIT_X; i++){
for (var j:int=0; j<LIMIT_Y; j++){
var rect:Sprite = new RandomRectFill(w,h,0x00FFFF,getColor(i,j));
rect.x = PADDING_X + (2*i + 1)*(w/2 + SPAN_X/2);
rect.y = PADDING_Y + (2*j + 1)*(h/2 + SPAN_Y/2);
_canvas.addChild(rect);
_rects.push(rect);
}
}
}
private function getColor(xNum:int,yNum:int):uint{
var r:int = 0;
var g:int = 128;
var b:int = 128*(LIMIT_X - 1 - xNum)/LIMIT_X + 128*(LIMIT_Y - 1 - yNum)/LIMIT_Y;
return r << 16 | g << 8 | b;
}
private function onClick(e:MouseEvent):void{
for each (var rect:RandomRectFill in _rects){
rect.randomize();
}
}
}
}
import flash.display.Graphics;
import flash.display.Sprite
import flash.geom.Point;
import flash.events.MouseEvent;
import flash.events.Event;
class RandomRectFill extends Sprite{
protected var _rectWidth:Number;
protected var _rectHeight:Number;
protected var _random:uint;
protected var _stroke:uint;
protected var _fillColor:uint;
protected var _points:Array;
/**
* Constructor
*/
public function RandomRectFill(width:Number,height:Number,
stroke:uint=0xFFFFFF,fillColor:uint=0xFFFFFF){
_rectWidth = width;
_rectHeight = height;
_stroke = stroke;
_fillColor = fillColor;
init();
}
protected function init():void{
var w:Number = _rectWidth;
var h:Number = _rectHeight;
var p1:Point = new Point( w/2, h/2);
var p2:Point = new Point(-w/2, h/2);
var p3:Point = new Point(-w/2, -h/2);
var p4:Point = new Point( w/2, -h/2);
_points = [p1,p2,p3,p4];
this.onBreak();
this.addEventListener(MouseEvent.ROLL_OVER,onBreak);
}
private function onBreak(e:MouseEvent=null):void{
_random = int(40*Math.random()) + 10;
this.addEventListener(Event.ENTER_FRAME,update);
}
protected function update(e:Event):void{
var g:Graphics = this.graphics;
g.clear();
g.lineStyle(0,_stroke);
for (var i:int=0; i<_points.length; i++){
var pStart:Point = _points[i];
var pEnd:Point = _points[i+1];
if (i == _points.length-1){
//The line from p4 to p1
pEnd = _points[0];
}
g.moveTo(pStart.x + randomGen(_random),pStart.y + randomGen(_random));
g.lineTo(pEnd.x + randomGen(_random),pEnd.y + randomGen(_random));
}
_random--;
if (_random < 1){
this.removeEventListener(Event.ENTER_FRAME,update);
this.drawSolidRect(_stroke,_fillColor);
}
}
//Create a ractangle without randomizers
protected function drawSolidRect(stroke:uint=0xFFFFFF,fillColor:uint=0xFFFFFF):void{
var w:Number = _rectWidth;
var h:Number = _rectHeight;
var g:Graphics = this.graphics;
g.clear();
g.lineStyle(0,stroke);
g.beginFill(fillColor);
g.drawRect(-w/2,-h/2,w,h);
g.endFill();
}
/**
* @return Random values between -random and random.
*/
protected function randomGen(random:uint):Number{
return 2*random*(Math.random() - 1/2);
}
public function randomize():void{
onBreak();
}
}