gpt4 book ai didi

actionscript-3 - Flex中的konami代码

转载 作者:行者123 更新时间:2023-12-03 08:03:52 24 4
gpt4 key购买 nike

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

最佳答案

状态机写起来很有趣,但是在这种情况下,我会使用签名模式。根据您想放置处理程序的位置(在组件的阶段),下面的一些代码应该可以工作,尽管您可以将其收紧(当然可以根据您的特定需求对其进行自定义):

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

关于actionscript-3 - Flex中的konami代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1546356/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com