【问题标题】:AS3 - make script wait for keystrokeAS3 - 让脚本等待击键
【发布时间】:2012-08-02 04:30:58
【问题描述】:

我需要我的脚本停止并等待,直到按下“输入”键,然后继续,如果不满足要求,则返回并再次等待。

它是我正在开发的游戏的登录屏幕,它需要等待用户按 Enter,然后检查提供的凭据并允许/拒绝它们。

        //log-in screen
        loginframe = new Login_Frame();
        addChild(loginframe);

        LoginToServer();

        function LoginToServer():Boolean
            {

                inputname = new TextField;
                inputname.type = TextFieldType.INPUT;
                inputname.border = true;
                inputname.x = 200;
                inputname.y = 200;
                inputname.height = 35;
                inputname.width = 545;
                inputname.multiline = false;
                inputname.text = "example";
                loginframe.addChild(inputname);


                inputpass = new TextField;
                inputpass.type = TextFieldType.INPUT;
                inputpass.border = true;
                inputpass.x = 200;
                inputpass.y = 300;
                inputpass.height = 35;
                inputpass.width = 545;
                inputpass.multiline = false;
                inputpass.text = "example";
                loginframe.addChild(inputpass);



                loginframe.addEventListener(KeyboardEvent.KEY_UP, hwndLogKeyboard);

                while (!loginsucsess)
                    {
                        //do nothing *this halts the compiler, takes 30+ seconds for window to appear after execution*
                        //and causes the debugger to shut off (due to delay fault)
                        //so i have three problems
                        //1. this is clearly not an accepable way to do this
                        //2. this code dosnt work, without my debugger i cant fix it
                        //3. even if the code did work, this is a huge project, i cant be goin without my debugger
                    }


                    return true;
            }

        function hwndLogKeyboard(evt:KeyboardEvent):void
            {
                if (evt.keyCode == 13)
                    {
                        if ((inputname.text == "tyler") && (inputpass.text == "shadowcopy"))
                            loginsucsess = true;
                    }
            }

我来自 C++ 背景,这个解决方案可以很好地工作,但 flash 似乎在转动拇指方面存在问题。

我当然尝试过询问 Google,但搜索结果甚至没有接近所需主题(我:AS3 等待按键 - Google:学习 Flash OOP!)

提前谢谢; 泰勒

【问题讨论】:

    标签: actionscript-3 keypress wait lag onkeydown


    【解决方案1】:

    阻止 UI 在任何语言中都不是最佳选择,除非您正在制作文本控制台应用程序。

    您的实现在您的 while() 语句中无限循环,直到达到最大脚本执行超时。

    改为使用异步设计模式:

    package
    {
        import flash.display.Sprite;
        import flash.events.Event;
        import flash.text.TextField;
    
        [SWF(percentWidth = 100, percentHeight = 100, backgroundColor = 0xefefef, frameRate = 60)]
        public class LoginForm extends Sprite
        {
    
            protected var username:TextField;
            protected var password:TextField;
    
            public function LoginForm()
            {
                super();
    
                // construct view
                username = new TextField();
                addChild(username);
    
                password = new TextField();
                addChild(password);
    
                // listen for change events from the text fields
                username.addEventListener(Event.CHANGE, loginChangeHandler);
                password.addEventListener(Event.CHANGE, loginChangeHandler);
            }
    
            protected function loginChangeHandler(event:Event):void
            {
                if ((username.text == "tyler") &&
                    (password.text == "shadowcopy"))
                {
                    // authentication verified - continue
                }
            }
    
        }
    }
    

    当任一文本字段值发生更改时,将针对您指定的身份验证凭据对其进行测试。如果满足,可以继续;否则,应用程序会在没有任何开销的情况下空闲。

    拥有一个登录按钮可能更符合用户体验。

    每次调试时,从 Flash Professional 转到“调试”菜单并选择“调试影片”。

    在 Flash Builder 中,右键单击项目应用程序并“调试为”或按工具栏中的调试按钮。基于 Eclipse 构建,您可能会发现它在代码编辑、调试和分析方面更加健壮。

    【讨论】:

    • 谢谢!我想这是一个简单的解决方案,让我眼前一亮。我必须对客户的其余代码进行大量更改才能使其正常工作,但现在一切正常。是的,我要添加一个“登录”按钮,我只是想首先为 devopment porpuses 最低限度地工作。再次感谢。
    猜你喜欢
    • 2010-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-26
    • 1970-01-01
    相关资源
    最近更新 更多