コロコロ文字
...
@author ...
// forked from mtok's コロコロ
// forked from mtok's 任意の点を中心に回転させる。サンプル
package
{
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.DisplayObject;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Matrix;
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.utils.Dictionary;
/**
* ...
* @author ...
*/
public class Tiling extends Sprite
{
private var boxes:Array;
private var mat:Matrix;
private var dic:Dictionary;
private var _background:Bitmap;
private var marginTop:Number = 7.5;
private var marginLeft:Number = 7.5;
private var msg:String = "楽しいFlash!";
public function Tiling()
{
addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);;
}
private function addedToStageHandler(e:Event):void
{
removeEventListener(e.type, arguments.callee);
stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
dic = new Dictionary(true);
var tf:TextField = new TextField();
tf.autoSize = TextFieldAutoSize.LEFT;
tf.text = msg.charAt(int(Math.random()*msg.length));
//trace(tf.width, tf.height);
var bd:BitmapData = new BitmapData(15, 15, true, 0x00000000);
var mat:Matrix = new Matrix();
mat.ty = -3;
bd.draw(tf, mat);
//addChild(new Bitmap(bd));
boxes = new Array();
var box:DisplayObject;
var c:uint;
for (var i:int = 0; i < bd.height; i++) {
for (var j:int = 0; j < bd.width; j++) {
c = bd.getPixel32(j, i);
if (c != 0) {
box = Box.createBox(30, 30);
box.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
box.x = 30 * int(15 * Math.random()) + marginLeft;
box.y = 30 * int(15 * Math.random()) + marginTop;
dic[box] = new Point(j, i);
//graphics.beginFill(0x00ff00);
//graphics.drawCircle(j*30 + 7.5, i*30 + 7.5, 7.5);
//graphics.endFill();
addChild(box);
boxes.push(box);
}
}
}
}
private function mouseDownHandler(e:MouseEvent):void
{
stage.removeEventListener(e.type, arguments.callee);
startAnimation();
//var b:Box = e.target as Box;
//b.moveUp();
//b.moveDown();
//b.moveRight();
//b.moveLeft();
}
private function startAnimation():void
{
var b:Box;
var p:Point;
for (var i:int = 0; i < boxes.length; i++) {
b = boxes[i] as Box;
b.addEventListener(Event.COMPLETE, boxCompleteHandler);
p = dic[b];
pokeBox(b, p);
}
}
private function boxCompleteHandler(e:Event):void
{
var b:Box = e.target as Box;
var p:Point = dic[b];
pokeBox(b, p);
}
private function pokeBox(b:Box, p:Point):void {
var pos:Point = getCellPosition(b);
var dx:Number = p.x - pos.x;
var dy:Number = p.y - pos.y;
if (dx == 0 && dy == 0) {
//trace(pos, p);
return;
}
if (Math.abs(dx) > Math.abs(dy)) {
if (dx > 0) {
b.moveRight();
}else {
b.moveLeft();
}
}else {
if (dy < 0) {
b.moveUp();
}else {
b.moveDown();
}
}
}
private function getCellPosition(b:Box):Point {
var r:Rectangle = b.getRect(b.parent); //trace(r.x - marginLeft, r.y - marginTop);
var p:Point = new Point(Math.round((r.x - marginLeft) / r.width), Math.round((r.y - marginTop)/r.height) );
return p;
}
}
}
import flash.events.MouseEvent;
import flash.events.Event;
import flash.display.Sprite;
import flash.display.DisplayObject;
import caurina.transitions.Tweener;
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.geom.Matrix;
internal class Box extends Sprite {
private var obj:Object;
public function Box() {
super();
}
public static function createBox(w:Number, h:Number, color:uint = 0xFD5322):Box {
var b:Box = new Box();
b.graphics.beginFill(0xffffff);
b.graphics.drawRect(0, 0, w, h);
b.graphics.endFill();
b.graphics.beginFill(color);
b.graphics.drawRect(2.5, 2.5, w-5, h-5);
b.graphics.endFill();
b.graphics.beginFill(0xFFFFFF);
b.graphics.drawCircle(w*0.25, w*0.35, 3.5);
b.graphics.drawCircle(w*0.75, w*0.35, 3.5);
//b.graphics.drawCircle(w*0.5, w*0.7, 2);
b.graphics.endFill();
b.graphics.beginFill(color);
b.graphics.drawCircle(w*0.25, w*0.38, 2);
b.graphics.drawCircle(w*0.75, w*0.38, 2);
//b.graphics.drawCircle(w*0.5, w*0.7, 1);
b.graphics.endFill();
b.buttonMode = true;
b.useHandCursor = true;
return b;
}
public function moveUp():void {
if (Tweener.isTweening(obj)) {
//trace('is tweening');
return;
}
obj = { angle:0 };
Tweener.addTween(obj, { angle:Math.PI*0.5, time:0.5, transition:"easeInOutCubic",
onStartScope:this,
onStart:function():void {
var rect:Rectangle = this.getRect(this.parent);
obj.matrix = this.transform.matrix;
obj.p = new Point(rect.x + rect.width, rect.y);
//trace('start',this.p);
},
onUpdateScope:this,
onUpdateParams:[obj],
onUpdate:onUpdate,
onCompleteScope:this,
onCompleteParams:[obj],
onComplete:onComplete
});
}
public function moveDown():void {
if (Tweener.isTweening(obj)) {
//trace('is tweening');
return;
}
var obj:Object = { angle:0 };
Tweener.addTween(obj, { angle:Math.PI*0.5, time:0.5, transition:"easeInOutCubic",
onStartScope:this,
onStart:function():void {
var rect:Rectangle = this.getRect(this.parent);
obj.matrix = this.transform.matrix;
obj.p = new Point(rect.x, rect.y + rect.height);
//trace('start',this.p);
},
onUpdateScope:this,onUpdateParams:[obj],onUpdate:onUpdate,
onCompleteScope:this,onCompleteParams:[obj],onComplete:onComplete
});
}
public function moveLeft():void {
if (Tweener.isTweening(obj)) {
//trace('is tweening');
return;
}
var obj:Object = { angle:0 };
Tweener.addTween(obj, { angle:Math.PI*0.5, time:0.5, transition:"easeInOutCubic",
onStartScope:this,
onStart:function():void {
var rect:Rectangle = this.getRect(this.parent);
obj.matrix = this.transform.matrix;
obj.p = new Point(rect.x, rect.y);
//trace('start',this.p);
},
onUpdateScope:this,onUpdateParams:[obj],onUpdate:onUpdate,
onCompleteScope:this,onCompleteParams:[obj],onComplete:onComplete
});
}
public function moveRight():void {
if (Tweener.isTweening(obj)) {
//trace('is tweening');
return;
}
var obj:Object = { angle:0, mc:this };
Tweener.addTween(obj, { angle:Math.PI*0.5, time:0.5, transition:"easeInOutCubic",
onStartScope:this,
onStart:function():void {
var rect:Rectangle = this.getRect(this.parent);
obj.matrix = this.transform.matrix;
obj.p = new Point(rect.x + rect.width, rect.y + rect.height);
},
onUpdateScope:this,onUpdateParams:[obj],onUpdate:onUpdate,
onCompleteScope:this,onCompleteParams:[obj],onComplete:onComplete
});
}
public function onComplete(params:Object):void {
Tweener.removeTweens(params);
dispatchEvent(new Event(Event.COMPLETE));
}
public function onUpdate(params:Object):void {
rotateAt(this, params);
}
public function isMoving():Boolean {
return obj != null;
}
private function rotateAt(d:DisplayObject, params:Object):void {
var m:Matrix = params.matrix.clone();
var m2:Matrix = new Matrix();
m2.translate( -params.p.x, -params.p.y);
m2.rotate(params.angle); //trace(params.angle);
m2.translate(params.p.x, params.p.y);
m.concat(m2);
d.transform.matrix = m;
}
}