actionscript 3 - codice konami in flex




actionscript-3 flash (2)

Quale sarebbe il modo migliore per implementare il codice konami in un'applicazione flessibile?

Voglio creare un componente per aggiungerlo a tutti i miei progetti, solo per divertimento.

Grazie

AGGIORNAMENTO: Ho realizzato un componente semplice, grazie a 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>

per testarlo

<?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>

Puoi usare Casalib . Ci sono classi, Key e KeyCombo . Puoi ascoltare KeyComboEvent.SEQUENCE .

Campione di lavoro:

<?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>

Una macchina a stati è divertente da scrivere, ma in questo caso andrei con un modello di firma. A seconda di dove vuoi mettere il gestore (sul palco del componente), ecco un codice che dovrebbe funzionare, anche se probabilmente puoi stringerlo (e ovviamente personalizzarlo secondo le tue esigenze specifiche):

// 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!
    }
}