tylerfb11
tylerfb11

Reputation: 43

AS3 - make script wait for keystroke

I need my script to stop and wait until the 'enter' key is pressed, then continue, and if requirments are are not met, to go back and wait again.

its a login screen to a game i am working on, it needs to wait for the user to press enter, then check the provided credidentails and allow/deny them assces.

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

I come from a C++ background where this solution would work just fine, but flash seems to have a problem with twidling its thumbs.

of course ive tryed asking Google, but the search results wernt anything even close to the required topic (me: AS3 wait for keypress - Google: Learn Flash OOP!) <-- no, bad Google.

thx in advance; Tyler

Upvotes: 0

Views: 1040

Answers (1)

Jason Sturges
Jason Sturges

Reputation: 15955

Blocking UI is not optimal in any language, unless you're making a text console application.

Your implementation infinitely loops within your while() statement until max script execution timeout is reached.

Instead, use asynchronous design patterns:

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

    }
}

When either of the text fields value changes, they are tested for the authentication credentials you have specified. If met, you can continue; otherwise, the application idles without any overhead.

Having a login button might be more inline with user experience.

Per debugging, from Flash Professional go to the "Debug" menu and select "Debug Movie".

From Flash Builder, right-click on the project application and "Debug as" or press the debug button from the toolbar. Built on Eclipse, you may find it more robust for code editing, debugging, and profiling.

Upvotes: 3

Related Questions