2009-10-09 55 views
35

將konami代碼實現到flex應用程序中的最佳方式是什麼?flex中的konami代碼

我想創建一個組件,將它添加到我的所有項目上,只是爲了好玩。

感謝

更新:我做了一個簡單的組件,這要歸功於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> 

測試它

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

回答

26

狀態機是好玩寫的,但在這種情況下,我有一個簽名模式去。這取決於你想要把處理程序(在組件的階段),這裏的一些代碼,應該工作,雖然你也許可以將其擰緊(當然它自定義您的具體需要):

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

只需添加一些處理「B」和「A」,這將是票。 – fenomas 2009-10-10 17:22:12

+0

感謝這工作很好,生病發布我的組件以後感興趣的公衆 – sergiogx 2009-10-10 21:17:43

+0

哎呀,你說得對。錯過了廣管局。 :-)好的,你得到了傑斯特!我會補充。 – ZaBlanc 2009-10-10 22:10:58

7

您可以使用Casalib。有類,KeyKeyCombo。您可能會收聽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> 
+0

任何想法,爲什麼這可能不適用於Flex 4? – Dan 2010-06-06 15:23:25

+0

腳本部分應該工作,因爲只有普通的AS3。 :) – 2010-06-07 08:02:46

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