でてきた順番にクリックする記憶ゲーム
オブジェクトを出た順番にクリックしていくゲーム。
@author SIBA
// write as3 code here..
package {
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
import flash.utils.Timer;
[SWF(width=800, height=600, backgroundColor=0xAADDFF)]
/**
* オブジェクトを出た順番にクリックしていくゲーム。
* @author SIBA
*/
public class Main01 extends Sprite {
// ----------------------------
// メンバ変数
// ----------------------------
private var objectList:Array = [];
// ----------------------------
// 初期化
// ----------------------------
public function Main01() {
var timer:Timer = new Timer(500, 10);
timer.addEventListener(TimerEvent.TIMER, onTimer);
timer.addEventListener(TimerEvent.TIMER_COMPLETE, onComplete);
timer.start();
}
// ----------------------------
// イベント
// ----------------------------
private function onTimer(event:TimerEvent):void {
var piece:Piece = new Piece(Math.random()*3);
do {
var isHit:Boolean = false;
piece.x = Math.random()*700+50;
piece.y = Math.random()*500+50;
piece.rotation = Math.random()*360;
for each (var tempPiece:Piece in objectList) {
if (tempPiece.hitTestObject(piece)) isHit = true;
}
} while (isHit);
addChild(piece);
objectList.push(piece);
}
private function onComplete(event:TimerEvent):void {
Timer(event.target).removeEventListener(TimerEvent.TIMER, onTimer);
Timer(event.target).removeEventListener(TimerEvent.TIMER_COMPLETE, onComplete);
for each (var piece:Piece in objectList) {
piece.addEventListener(MouseEvent.CLICK, onClick);
}
}
private function onClick(event:MouseEvent):void {
var piece:Piece = objectList.shift();
piece.removeEventListener(MouseEvent.CLICK, onClick);
if (event.target != piece) {
var txtGameOver:TextField = new TextField();
txtGameOver.x = 350;
txtGameOver.y = 200;
txtGameOver.autoSize = TextFieldAutoSize.CENTER;
txtGameOver.defaultTextFormat = new TextFormat(null, 120);
txtGameOver.text = "GAME OVER";
addChild(txtGameOver);
for each (var tempPiece:Piece in objectList) {
tempPiece.removeEventListener(MouseEvent.CLICK, onClick);
}
}
else {
removeChild(piece);
}
}
}
}
// ------------------------------------
// 内部クラス
// ------------------------------------
import flash.display.Sprite;
import flash.geom.Point;
class Piece extends Sprite {
// --------------------------------
// 初期化
// --------------------------------
public function Piece(style:int) {
graphics.lineStyle(10, Math.random()*0xFFFFFF);
graphics.beginFill(Math.random()*0xFFFFFF, 0.5);
switch (style) {
case 0:
createCircle();
break;
case 1:
createRectangle();
break;
default:
createTriangle();
break;
}
graphics.endFill();
}
private function createCircle():void {
graphics.drawCircle(0, 0, 50);
}
private function createRectangle():void {
graphics.drawRect(-50, -50, 100, 100);
}
private function createTriangle():void {
for (var i:int=0; i<4; i++) {
var p:Point = Point.polar(50, i*120*Math.PI/180);
if (i == 0) graphics.moveTo(p.x, p.y);
else graphics.lineTo(p.x, p.y);
}
}
}