本文介绍了flex中的konami代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在 flex 应用程序中实现 konami 代码的最佳方法是什么?
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.
谢谢
更新:感谢 ZaBlanc,我制作了一个简单的组件
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>
测试一下
<?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>
推荐答案
状态机写起来很有趣,但在这种情况下,我会使用签名模式.根据您想将处理程序放置的位置(在组件的舞台上),这里有一些应该可以工作的代码,但您可能可以收紧它(当然还可以根据您的特定需要对其进行自定义):
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!
}
}
这篇关于flex中的konami代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!