KonamiCommand.as (HIGE version)
コナミコマンド.as
コナミコマンドを入力してみよう。 上上下下左右左右BA
@author jc at bk-zen.com
/**
* Copyright bkzen ( http://wonderfl.net/user/bkzen )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/cAHJ
*/
// forked from bkzen's ひげ
package
{
import flash.display.BitmapData;
import flash.display.DisplayObject;
import flash.display.GradientType;
import flash.display.Graphics;
import flash.display.Shape;
import flash.display.SimpleButton;
import flash.display.Sprite;
import flash.events.Event;
import flash.geom.Matrix;
/**
* コナミコマンド.as
* コナミコマンドを入力してみよう。 上上下下左右左右BA
* @author jc at bk-zen.com
*/
[SWF(width = "465", height = "465", backgroundColor = "0xFFFFFF", frameRate = "60")]
public class KonamiCmd extends Sprite
{
public static const KONAMI_COMMAND: String = "38|38|40|40|37|39|37|39|66|65";
private var guide:Shape;
public function KonamiCmd()
{
super();
if (stage) setup();
else addEventListener(Event.ADDED_TO_STAGE, setup);
}
private function setup(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, setup);
//
KeyEventManager.init(stage);
var btn: SimpleButton = new SimpleButton(upState(), overState());
btn.hitTestState = upState();
addChild(btn);
var cmd: KeyCommand = new KeyCommand(KONAMI_COMMAND);
trace(cmd.length);
cmd.addEventListener(Event.COMPLETE, konamiCommand);
guide = new Shape();
addChild(guide);
makeFace();
//cmd.addEventListener();
}
private function makeFace(size: Number = 2):void
{
var g: Graphics = guide.graphics;
g.clear();
var minX: Number = 116, minY: Number = 116, maxX: Number = 349, maxY: Number = 349;
var w: Number = maxX - minX;
var h: Number = maxY - minY;
guide.x = minX;
guide.y = minY;
g.clear();
g.lineStyle(1, 0x333333);
g.drawEllipse(0, 0, w, h);
g.moveTo(w / 5 * 1.3, h / 4);
g.lineTo(w / 5 * 1.7, h / 4);
g.moveTo(w / 5, h / 5);
g.lineTo(w / 5 * 2, h / 5);
g.moveTo(w / 5 * 3.3, h / 4);
g.lineTo(w / 5 * 3.7, h / 4);
g.moveTo(w / 5 * 3, h / 5);
g.lineTo(w / 5 * 4, h / 5);
g.moveTo(w / 2, h / 5 * 3);
g.lineTo(w / 2, h / 5 * 2);
g.moveTo(w / 5 * 1.5, h / 5 * 4);
g.lineTo(w / 5 * 3.5, h / 5 * 4);
g.endFill();
// ひげエリア
var higeArea: Shape = new Shape();
var matrix: Matrix = new Matrix();
matrix.createGradientBox(w, h, 90 * Math.PI / 180);
higeArea.graphics.beginGradientFill(GradientType.LINEAR, [0xFFFFFF, 0x333333], [1.0, 1.0], [0xCC, 0xFF], matrix);
higeArea.graphics.drawEllipse(0, 0, w, h);
var dh: int = h / 0xFF * (0xFF - 0xCC);
var bmd: BitmapData = new BitmapData(w, dh, true, 0xFFFFFFFF);
bmd.lock();
matrix = new Matrix();
matrix.ty = dh - h;
bmd.draw(higeArea, matrix);
bmd.unlock();
var n: int = bmd.width * bmd.height;
for (var i: int = 0; i < n; i++ )
{
var x: int = i % bmd.width;
var y: int = i / bmd.width;
var c: uint = bmd.getPixel32(x, y);
if (c != 0xFFFFFFFF)
{
var b: int = c & 0xFF;
var whiskerL: Number = Math.random() * (0xFF - b) / 0xFF * size;
var whiskerR: Number = Math.random() * 2 * Math.PI;
var acX: Number = x + whiskerL * Math.cos(whiskerR);
var acY: Number = y + whiskerL * Math.sin(whiskerR) + h - dh;
var ctrlD: Number = whiskerL * Math.random();
var ctrlR: Number = Math.random() * 2 * Math.PI;
var ctrlX: Number = x + ctrlD * Math.cos(ctrlR);
var ctrlY: Number = y + ctrlD * Math.sin(ctrlR) + h - dh;
g.lineStyle(1, 0, (0xFF - b) / 0xFF);
g.moveTo(x, y + h - dh);
g.curveTo(ctrlX, ctrlY, acX, acY);
}
}
bmd.dispose();
}
private function overState(): DisplayObject
{
var sp: Sprite = new Sprite();
sp.graphics.beginFill(0x0099CC);
sp.graphics.drawRect(0, 0, 20, 20);
return sp;
}
private function upState(): DisplayObject
{
var sp: Sprite = new Sprite();
sp.graphics.beginFill(0x006699);
sp.graphics.drawRect(0, 0, 20, 20);
return sp;
}
private function konamiCommand(e:Event):void
{
makeFace(100);
}
}
}
import flash.display.InteractiveObject;
import flash.display.Stage;
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.FocusEvent;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
class KeyCommand extends EventDispatcher
{
private var cmd: String;
private var cmds: Array = [];
private var p: int = -1;
public function KeyCommand(cmd: String = "")
{
if (cmd != "") cmds = cmd.split(/[^0-9]+/);
this.cmd = cmds.join("|");
KeyEventManager.getManager().addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
}
public function addCmd(cmd: String): void
{
var arr: Array = cmd.split(/[^0-9]+/);
cmds.push.apply(null, arr);
cmd = cmds.join("|");
}
private function onKeyUp(e: KeyboardEvent): void
{
p++;
if (cmds[p] != e.keyCode) p = -1;
if (p == cmds.length - 1)
{
p = -1;
dispatchEvent(new Event(Event.COMPLETE));
}
}
public function get length(): int
{
return cmds.length;
}
}
class KeyEventManager extends EventDispatcher
{
private var stg: Stage;
private var focus: InteractiveObject;
private static var _manager: KeyEventManager;
public static function getManager(): KeyEventManager
{
if (_manager == null) _manager = new KeyEventManager();
return _manager;
}
public static function init(stage: Stage): void
{
getManager().stg = stage;
getManager().setup();
}
private function setup(): void
{
stg.addEventListener(FocusEvent.FOCUS_IN, onFocusChange);
stg.addEventListener(FocusEvent.FOCUS_OUT, onFocusChange);
stg.addEventListener(MouseEvent.CLICK, onClickStage);
if (stg.focus)
{
focus = stg.focus;
stg.focus.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
}
else
{
focus = stg;
stg.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
}
}
private function onClickStage(e:MouseEvent):void
{
onFocusChange();
}
private function onFocusChange(e: FocusEvent = null): void
{
if (focus == stg.focus) return;
focus.removeEventListener(KeyboardEvent.KEY_UP, onKeyUp);
focus = (stg.focus ? stg.focus : stg);
focus.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
}
private function onKeyUp(e: KeyboardEvent): void
{
dispatchEvent(e.clone());
}
}