WanderingDot
クリックすると動き出します
@author @kndys
package
{
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.ColorTransform;
import flash.geom.Matrix;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
/**
* クリックすると動き出します
* @author @kndys
*/
public class Main extends Sprite
{
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
// entry point
var dots:Vector.<WanderingDot> = new Vector.<WanderingDot>();
var bmd:BitmapData = new BitmapData(465, 465);
addChild(new Bitmap(bmd));
var ct:ColorTransform = new ColorTransform(1.05, 1.05, 1.05, 1, 8, 8, 8, 0);
var tf:TextField = new TextField();
tf.defaultTextFormat = new TextFormat( null, 20, 0);
tf.autoSize = TextFieldAutoSize.CENTER;
tf.text = "えびぞうさんぼうこうじけん";
bmd.draw(tf, new Matrix(1, 0, 0, 1, 220, 220));
for (var k:int = 0; k < bmd.height; k+=2)
{
for (var l:int = 0; l < bmd.width; l+=2)
{
if (bmd.getPixel(l, k) == 0)
dots.push(new WanderingDot(l, k));
}
}
stage.addEventListener(MouseEvent.CLICK, function(e:Event):void
{
stage.removeEventListener(MouseEvent.CLICK, arguments.callee);
addEventListener(Event.ENTER_FRAME, function(e:Event):void
{
bmd.colorTransform(bmd.rect, ct);
for (var j:int = 0; j < dots.length; j++)
{
var item:WanderingDot = dots[j];
bmd.setPixel32(item.x, item.y, 0xff000000);
item.update((4 * Math.random()) | 0);
}
});
});
}
}
}
internal class WanderingDot
{
private static var _xMin:int = 0;
private static var _xMax:int = 465;
private static var _yMin:int = 0;
private static var _yMax:int = 465;
private var _vx:int;
private var _vy:int;
private var _px:int;
public function get x():int { return _px; }
public function set x(value:int):void
{
_px = value;
}
private var _py:int;
public function get y():int { return _py; }
public function set y(value:int):void
{
_py = value;
}
public function WanderingDot(x:int = 0, y:int = 0)
{
_vx = 1;
_vy = 1;
_px = x;
_py = y;
_validate();
}
private function _validate():void
{
if (_px < _xMin)
{
_px = _xMin;
_vx = 1;
}
else
if (_px > _xMax)
{
_px = _xMax;
_vx = -1;
}
if (_py < _yMin)
{
_py = _yMin;
_vy = 1;
}
else
if (_py > _yMax)
{
_py = _yMax;
_vy = -1;
}
}
public function update(n:uint):void
{
switch(n)
{
default:
case 0:
case 2:
break;
case 1:
if (_vx != 0 && _vy == 0)
{
_vy = 1;
}
else
if (_vx == 0 && _vy != 0)
{
_vx = 1;
}
else
{
_vx = 0;
}
break;
case 3:
if (_vx != 0 && _vy == 0)
{
_vy = -1;
}
else
if (_vx == 0 && _vy != 0)
{
_vx = -1;
}
else
{
_vy = 0;
}
break;
}
_px += _vx;
_py += _vy;
_validate();
}
}