将konami代码实现到flex应用程序的最佳方法是什么?
我想创建一个组件,将其添加到我的所有proyects上,只是为了好玩。
感谢
更新:由于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>
答案 0 :(得分: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 :(得分:7)
您可以使用Casalib。有一些课程Key
和KeyCombo
。您可以收听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>
答案 2 :(得分: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);
}
}