actionscript-3 - codice konami in flex




flash (4)

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>

Answers

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);
    }
}

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

Puoi usare css per applicare uno skin a una classe di componenti in questo modo:

@namespace component "com.domain.project.view.component.*";

component|CustomComponent {
    skin-class: ClassReference("com.domain.project.view.skin.CustomSkin");
}

O se lo vuoi all'interno del tuo codice componente, puoi impostare lo stile nel costruttore:

public function CustomComponent()
{
  super();
  setStyle("skinClass", CustomSkin);
}