在flex的konami代码
将konami代码实现到flex应用程序的最佳方法是什么?
我想创build一个组件,将其添加到我的所有项目,只是为了好玩。
谢谢
更新:我做了一个简单的组件,感谢ZaBlanc
<?xml version="1.0" encoding="utf-8"?> <mx:UIComponent xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="init()"> <mx:Metadata> [Event(name="success", type="flash.events.Event")] </mx:Metadata> <mx:Script> <![CDATA[ // up-up-down-down-left-right-left-right-BA public static const KONAMI_CODE:String = "UUDDLRLRBA"; // signature private var signatureKeySequence:String = ""; private function init():void{ systemManager.stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown); } private function onKeyDown(event:KeyboardEvent):void{ var keyCode:int = event.keyCode; switch (keyCode) { case Keyboard.UP: signatureKeySequence += "U"; break; case Keyboard.DOWN: signatureKeySequence += "D"; break; case Keyboard.LEFT: signatureKeySequence += "L"; break; case Keyboard.RIGHT: signatureKeySequence += "R"; break; case 66: //Keyboard.B only for AIR :/ signatureKeySequence += "B"; break; case 65: //Keyboard.A only for AIR too :( signatureKeySequence += "A"; break; default: signatureKeySequence = ""; break; } // crop sequence signatureKeySequence = signatureKeySequence.substr(0, KONAMI_CODE.length); // check for konami code if (signatureKeySequence == KONAMI_CODE) { dispatchEvent(new Event("success")); signatureKeySequence = ""; } } ]]> </mx:Script> </mx:UIComponent>
testing它
<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" xmlns:konamicode="konamicode.*"> <mx:Script> <![CDATA[ import mx.controls.Alert; ]]> </mx:Script> <konamicode:KonamiCodeCatch success="Alert.show('+30 lives!!!')" /> </mx:Application>
一个状态机写起来很有趣,但是在这种情况下,我会使用签名模式。 取决于你想把处理器放在哪里(在组件的舞台上),下面是一些应该可以工作的代码,虽然你可能会收紧它(当然也可以根据你的具体需要来定制它):
// up-up-down-down-left-right-left-right-BA public static const KONAMI_CODE:String = "UUDDLRLRBA"; // signature private var signatureKeySequence:String = ""; private function onKeyDown(event:KeyboardEvent):void { var keyCode:int = event.keyCode; switch (keyCode) { case Keyboard.UP: signatureKeySequence += "U"; break; case Keyboard.DOWN: signatureKeySequence += "D"; break; case Keyboard.LEFT: signatureKeySequence += "L"; break; case Keyboard.RIGHT: signatureKeySequence += "R"; break; case Keyboard.B: signatureKeySequence += "B"; break; case Keyboard.A: signatureKeySequence += "A"; break; default: signatureKeySequence = ""; break; } // crop sequence signatureKeySequence = signatureKeySequence.substr(0, KONAMI_CODE.length); // check for konami code if (signatureKeySequence == KONAMI_CODE) { // 30 lives! } }
你可以使用Casalib 。 有类, Key
和KeyCombo
。 你可以听KeyComboEvent.SEQUENCE
。
工作样本:
<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" initialize="init();"> <mx:Script> <![CDATA[ import mx.controls.Alert; import org.casalib.events.KeyComboEvent; import org.casalib.ui.Key; import org.casalib.ui.KeyCombo; import org.casalib.util.StageReference; private const KONAMI_CODE:KeyCombo = new KeyCombo([Keyboard.UP,Keyboard.UP,Keyboard.DOWN,Keyboard.DOWN,Keyboard.LEFT,Keyboard.RIGHT,Keyboard.LEFT,Keyboard.RIGHT,("B").charCodeAt(),("A").charCodeAt()]); private function init():void { StageReference.setStage(this.systemManager.stage); Key.getInstance().addKeyCombo(KONAMI_CODE); Key.getInstance().addEventListener(KeyComboEvent.SEQUENCE,onKonami); } private function onKonami(evt:KeyComboEvent):void { if (evt.keyCombo == KONAMI_CODE){ Alert.show("You know Konami code?","WOW"); } } ]]> </mx:Script> </mx:Application>
var unlockCode:Array = new Array(38,38,40,40,37,39,37,39,66,65,13); var keyPressArray:Array = new Array(); stage.addEventListener(KeyboardEvent.KEY_DOWN, checkUnlockCode); function checkUnlockCode(e:KeyboardEvent):void { if (keyPressArray.length >= unlockCode.length) { keyPressArray.splice(0,1); keyPressArray.push(e.keyCode.toString()); } else { keyPressArray.push(e.keyCode.toString()); } trace(keyPressArray); if (keyPressArray.toString() == unlockCode.toString()) { trace(unlockCode); } }