0

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

它是我正在开发的游戏的登录屏幕,它需要等待用户按 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!)<--不,糟糕的 Google。

提前谢谢;泰勒

4

1 回答 1

3

阻止 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 构建,您可能会发现它在代码编辑、调试和分析方面更加健壮。

于 2012-08-02T05:37:09.753 回答