Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

konami code in flex

What would be the best way to implement the konami code into a flex application?

I want to create a component to add it on all my proyects, just for fun.

thanks

UPDATE: I made a simple component, thanks to 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-B-A             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> 

to test it

<?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> 
like image 960
sergiogx Avatar asked Oct 09 '09 22:10

sergiogx


1 Answers

A state machine is fun to write, but in this case I'd go with a signature pattern. Depending on where you want to put the handler (on the stage of the component), here's some code that should work, though you can probably tighten it (and of course customize it to your specific need):

// up-up-down-down-left-right-left-right-B-A 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!     } } 
like image 182
ZaBlanc Avatar answered Oct 05 '22 20:10

ZaBlanc