ぱたぱたパネル
package
{
import flash.display.Sprite;
public class PataPata extends Sprite
{
public function PataPata() {
var vec:Vector.<Vector.<Sq>> = new Vector.<Vector.<Sq>>;
var sq:Sq;
var i :int, j:int;
for ( i = 0; i < 32; i++) {
vec[i] = new Vector.<Sq>;
for ( j = 0; j < 32; j++) {
sq = new Sq();
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
);
}
}
}
}
}
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;
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 C_HEAD:uint = 0x80ff80;
private static const C_TAIL:uint = 0xff80ff;
private var face:String;
private var steps :int;
private var tile : Sprite;
private var graph: Graphics;
private var timer: Timer;
public function Sq() {
face = HEAD;
steps = -1;
tile = new Sprite();
graph = tile.graphics;
addChild(tile);
graph.beginFill(C_HEAD, 1);
graph.lineStyle(0, 0);
graph.drawRect(0, 0, 15, 15);
tile.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(new DataEvent(REVERSE, false, false, this.face));
}
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 <= 15) {
graph.clear();
graph.beginFill(C_HEAD, 1);
graph.lineStyle(0, 0);
graph.drawRect(0, 0.5*steps, 15, 15-steps);
} else if (steps <= 30) {
graph.clear();
graph.beginFill(C_TAIL, 1);
graph.lineStyle(0, 0);
graph.drawRect(0, 15.0-0.5*steps , 15, steps-15);
} else {
removeEventListener (Event.ENTER_FRAME, faceTail);
}
}
private function faceHead(e:Event):void {
steps--;
if (steps >= 15) {
graph.clear();
graph.beginFill(C_TAIL, 1);
graph.lineStyle(0, 0);
graph.drawRect(0, 0.5*steps, 15, 15-steps);
} else if (steps >= 0) {
graph.clear();
graph.beginFill(C_HEAD, 1);
graph.lineStyle(0, 0);
graph.drawRect(0, 15.0-0.5*steps , 15, steps-15);
} 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(new DataEvent(REVERSE, false, false, this.face));
}
}