ドット違い探しゲーム
GAME :: ドット違い探しゲーム
*
* どれかひとつだけ、他と1ドット異なるものがあります。ひたすら探し続けます。
/**
* Copyright daniwell ( http://wonderfl.net/user/daniwell )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/uXLU
*/
/**
* GAME :: ドット違い探しゲーム
*
* どれかひとつだけ、他と1ドット異なるものがあります。ひたすら探し続けます。
*/
package
{
import caurina.transitions.Tweener;
import flash.display.Sprite;
import flash.display.BitmapData;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Point;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.utils.getTimer;
[SWF(backgroundColor = "0xffffff", frameRate = "30", width = "465", height = "465")]
public class DotDiff extends Sprite
{
private const NUM_W :uint = 6; // ヨコ 個数
private const NUM_H :uint = 6; // タテ 個数
private const SIZE :uint = 4; // SIZE * SIZE ドット
private var img :BitmapData;
private var wrongImg :BitmapData;
private var wrongNum :uint;
private var sp :Sprite;
private var panels :Vector.<Vector.<Panel>>;
private var startTime:Number;
private var time:Number;
private var status :Status;
public function DotDiff()
{
generateBitmapData();
startTime = getTimer();
sp = new Sprite();
panels = new Vector.<Vector.<Panel>>(NUM_H);
for (var i:int = 0; i < NUM_H; i ++)
{
panels[i] = new Vector.<Panel>(NUM_W);
for (var j:int = 0; j < NUM_W; j ++)
{
var p :Panel = new Panel();
p.setBitmapData(img);
p.x = j * 40;
p.y = i * 40;
p.id = i * NUM_W + j;
p.addEventListener(MouseEvent.CLICK, clickHandler);
sp.addChild(p);
panels[i][j] = p;
}
}
sp.x = int((465 - sp.width) / 2);
sp.y = 50;
addChild(sp);
wrongNum = int(Math.random() * NUM_W * NUM_H);
panels[int(wrongNum / NUM_W)][wrongNum % NUM_W].setBitmapData(wrongImg);
status = new Status();
status.x = sp.x;
status.y = sp.y + sp.height + 20;
addChild(status);
var tf :TextField = new TextField();
tf.x = sp.x;
tf.y = 10;
tf.autoSize = "left";
tf.defaultTextFormat = new TextFormat("_sans", 11, 0x0);
tf.text = "他のものと異なるカタチのものを選んでください。";
addChild(tf);
stage.addEventListener(Event.ENTER_FRAME, function():void { status.setTime((getTimer() - startTime)) } );
}
private function clickHandler(e:MouseEvent):void
{
if (e.target.id == wrongNum) // 正解時
{
status.setAverage();
generateBitmapData();
sp.alpha = 0;
Tweener.addTween(sp, { alpha:1, time:0.3, transition:"linear" } );
panels[int(wrongNum / NUM_W)][wrongNum % NUM_W].setBitmapData(img);
wrongNum = int(Math.random() * NUM_W * NUM_H);
panels[int(wrongNum / NUM_W)][wrongNum % NUM_W].setBitmapData(wrongImg);
}
else startTime -= 1000; // 間違えた時のペナルティ (1 sec)
}
private function generateBitmapData ( ) :void
{
if (!img) img = new BitmapData(SIZE,SIZE,false,0xffffff);
if (!wrongImg) wrongImg = new BitmapData(SIZE, SIZE, false, 0xffffff);
for (var i:int = 0; i < SIZE; i ++){
for (var j:int = 0; j < SIZE; j ++){
if (Math.random() < 0.5) img.setPixel(i, j, 0x000000);
else img.setPixel(i, j, 0xffffff);
}
}
wrongImg.copyPixels(img, img.rect, new Point());
var r :uint = int(Math.random() * SIZE * SIZE);
wrongImg.setPixel(r % SIZE, int(r / SIZE), ~(img.getPixel(r % SIZE, int(r / SIZE))) );
}
}
}
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.geom.ColorTransform;
import flash.text.TextField;
import flash.text.TextFormat;
class Status extends Sprite
{
private var _time :Number;
private var _count :uint = 0;
private var _tfTime :TextField;
private var _tfAverage :TextField;
public function Status()
{
_tfAverage = createTextField();
_tfTime = createTextField();
_tfAverage.y = 13;
_tfAverage.text = " ";
_tfAverage.height = 20;
addChild(_tfAverage);
addChild(_tfTime);
}
public function setTime(n:Number):void
{
_time = n;
_tfTime.text = "total time: " + convert(n);
}
public function setAverage():void
{
_count ++;
_tfAverage.text = " ave time: " + convert(_time/_count);
}
private function convert(n:Number):String
{
var s :String = "." + digit((n % 1000) / 10);
n = int(n / 1000);
s = digit(n / 60) + ":" + digit(n % 60) + s;
return s;
}
private function digit (n:int) :String
{
return (9 < n)? String(n):"0" + n;
}
private function createTextField():TextField
{
var format :TextFormat = new TextFormat("_等幅", 10, 0x0);
format.letterSpacing = 1;
var tf :TextField = new TextField();
tf.autoSize = "left";
tf.defaultTextFormat = format;
return tf;
}
}
class Panel extends Sprite
{
public var id :uint;
private var _bmp :Bitmap;
public function Panel()
{
scaleX = scaleY = 6;
_bmp = new Bitmap();
addChild(_bmp);
buttonMode = true;
addEventListener(MouseEvent.MOUSE_OVER, function():void { transform.colorTransform = new ColorTransform(0.8, 0.8, 0.8); } );
addEventListener(MouseEvent.MOUSE_OUT, function():void { transform.colorTransform = new ColorTransform(); } );
}
public function setBitmapData(bmd:BitmapData):void { _bmp.bitmapData = bmd; }
}