flex中的konami代码
Posted
技术标签:
【中文标题】flex中的konami代码【英文标题】:konami code in flex 【发布时间】:2010-12-05 12:07:03 【问题描述】:将 konami 代码实现到 flex 应用程序中的最佳方式是什么?
我想创建一个组件来将它添加到我所有的项目中,只是为了好玩。
谢谢
更新:感谢 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>
【问题讨论】:
【参考方案1】:您可以使用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>
【讨论】:
脚本部分应该可以正常工作,因为只有普通的 AS3。 :)【参考方案2】:状态机写起来很有趣,但在这种情况下,我会使用签名模式。根据您希望将处理程序放在哪里(在组件的舞台上),这里有一些应该可以工作的代码,尽管您可以收紧它(当然也可以根据您的特定需要对其进行自定义):
// 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!
【讨论】:
只需为“B”和“A”添加一些处理,这就是票。 感谢这个效果很好,稍后我会为感兴趣的公众发布我的组件 糟糕,没错。错过了BA。 :-) 好的,你明白了!我会补充的。 我认为没有 Keyboard.B 和 Keyboard.A :P 请参阅我的答案 :) 根据您想要的灵活性,您可能需要测试最后输入的 10 个字符而不是前 10 个字符。因此,如果用户在中间输入 UUUDDLRLRBA 或错误输入,他们可以重新开始'仍然有效。像这样:signatureKeySequence = signatureKeySequence.substr(signatureKeySequence - KONAMI_CODE.length);【参考方案3】: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);
【讨论】:
以上是关于flex中的konami代码的主要内容,如果未能解决你的问题,请参考以下文章
html Unicorn Shitting Rainbows - Konami代码开始有趣