forked from: ぱたぱたパネル
これ短い間隔でクリックすると変なことになりません?
// forked from knd's ぱたぱたパネル
package
{
import flash.display.Sprite;
import flash.display.Shape;
import flash.display.BitmapData;
import flash.display.Graphics;
import net.hires.debug.Stats;
// これ短い間隔でクリックすると変なことになりません?
public class PataPata extends Sprite
{
private static const C_HEAD:uint = 0x80ff80;
private static const C_TAIL:uint = 0xff80ff;
public function PataPata() {
var cache : Vector.<BitmapData> = new Vector.<BitmapData>();
var s : Shape = new Shape();
var i : int = 0;
var bmd : BitmapData;
var g : Graphics = s.graphics;
for(i = 0;i < 15;i++){
bmd = new BitmapData(15, 15, false);
g.clear();
g.beginFill(C_HEAD, 1);
g.lineStyle(0, 0);
g.drawRect(0, 0.5*i, 15, 15-i);
bmd.draw(s);
cache.push(bmd);
}
for(i = 14;i >= 0;i--){
bmd = new BitmapData(15, 15, false);
g.clear();
g.beginFill(C_TAIL, 1);
g.lineStyle(0, 0);
g.drawRect(0, 0.5*i, 15, 15-i);
bmd.draw(s);
cache.push(bmd);
}
var vec:Vector.<Vector.<Sq>> = new Vector.<Vector.<Sq>>;
var sq:Sq;
var j:int;
for ( i = 0; i < 32; i++) {
vec[i] = new Vector.<Sq>;
for ( j = 0; j < 32; j++) {
sq = new Sq(cache);
sq.x = i * 15;
sq.y = j * 15;
addChild(sq);
vec[i].push(sq);
}
}
for ( i = 0; i < 32; i++) {
for ( j = 0; j < 32; j++) {
sq = vec[i][j];
sq.setRelation(
i >= 1 ? vec[i - 1][j]: null,
i <= 30 ? vec[i + 1][j]:null,
j <= 30 ? vec[i][j + 1]:null,
j >= 1? vec[i][j - 1]:null
);
}
}
addChild(new Stats());
}
}
}
import flash.display.Graphics;
import flash.display.Shape;
import flash.display.Sprite;
import flash.events.DataEvent;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.utils.Timer;
import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.geom.Matrix;
class Sq extends Sprite {
private static const HEAD:String = "head";
private static const TAIL:String = "tail";
private static const REVERSE:String = "reverse";
private static const E_HEAD : DataEvent = new DataEvent(REVERSE, false, false, HEAD);
private static const E_TAIL : DataEvent = new DataEvent(REVERSE, false, false, TAIL);
private var face:String;
private var steps :int;
private var timer: Timer;
private var _cache : Vector.<BitmapData>;
private var _bmp : Bitmap;
public function Sq(cache : Vector.<BitmapData>) {
_cache = cache;
face = HEAD;
steps = -1;
_bmp = new Bitmap(_cache[0]);
addChild(_bmp);
addEventListener(MouseEvent.CLICK, click);
}
public function setRelation(north:Sq, south: Sq, east: Sq, west:Sq):void {
if (north != null) {
north.addEventListener(REVERSE, notice);
}
if (south != null) {
south.addEventListener(REVERSE, notice);
}
if (east != null) {
east.addEventListener(REVERSE, notice);
}
if (west != null) {
west.addEventListener(REVERSE, notice);
}
}
private function notice(e:DataEvent) :void {
if (e.data != this.face) {
this.face = e.data;
timer = new Timer(50, 1);
timer.addEventListener(TimerEvent.TIMER_COMPLETE, delay);
timer.start();
}
}
private function delay(e:TimerEvent):void {
if (timer&&!timer.running) {
timer = null;
}
reverse(this.face);
dispatchEvent(this.face == HEAD ? E_HEAD : E_TAIL);
}
private function reverse(nextFace:String):void {
if (hasEventListener(Event.ENTER_FRAME)) {
removeEventListener(Event.ENTER_FRAME, faceHead);
removeEventListener (Event.ENTER_FRAME, faceTail);
}
if (nextFace == TAIL){
addEventListener(Event.ENTER_FRAME, faceTail);
}else if(nextFace == HEAD){
addEventListener(Event.ENTER_FRAME, faceHead);
}
}
private function faceTail(e:Event):void {
steps++;
if (steps < 29) {
if(steps >= 0)_bmp.bitmapData = _cache[steps];
} else {
removeEventListener (Event.ENTER_FRAME, faceTail);
}
}
private function faceHead(e:Event):void {
steps--;
if (steps > 0) {
if(steps < 30)_bmp.bitmapData = _cache[steps];
} else {
removeEventListener(Event.ENTER_FRAME, faceHead);
}
}
private function click(e:MouseEvent):void {
if (this.face == TAIL)
this.face = HEAD;
else if (this.face == HEAD)
this.face = TAIL;
reverse(this.face);
dispatchEvent(this.face == HEAD ? E_HEAD : E_TAIL);
}
}