34

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>
animuson
  • 53,861
  • 28
  • 137
  • 147
sergiogx
  • 1,562
  • 1
  • 19
  • 36

3 Answers3

26

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!
    }
}
ZaBlanc
  • 4,679
  • 1
  • 35
  • 38
  • 1
    Just add in some handling for "B" and "A" and this'll be the ticket. – fenomas Oct 10 '09 at 17:22
  • thanks this worked great, ill post my component later for the interested public – sergiogx Oct 10 '09 at 21:17
  • Oops, yer right. Missed the BA. :-) OK, you get the jist! I'll add. – ZaBlanc Oct 10 '09 at 22:10
  • I don't think there are Keyboard.B and Keyboard.A :P See my answer for that :) – Andy Li Oct 11 '09 at 19:07
  • 1
    Depending on how flexible you want it, you might have to test the last 10 characters entered instead of the first 10. So if a user types UUUDDLRLRBA or mistypes in the middle, they can start over and it'll still be valid. Something like this: signatureKeySequence = signatureKeySequence.substr(signatureKeySequence - KONAMI_CODE.length); – Newtang Jan 14 '10 at 03:32
7

You may use Casalib. There are classes, Key and KeyCombo. You may listen for KeyComboEvent.SEQUENCE.

Working sample:

<?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>
Andy Li
  • 5,894
  • 6
  • 37
  • 47
1
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);
    }
}
Brett
  • 11
  • 1