Skip to main content
Participant
October 10, 2009
Answered

Beginner's trouble with If-Else

  • October 10, 2009
  • 1 reply
  • 688 views

I am just learning AS. I am trying to make a username and password box that will return a welcome message if the two text fields are entered correctly and a “Sorry Charlie” message if they are not.

I am using an If-Else statement. If I have the Else part commented out, I am able to enter text into the two text fields and get a welcome message. With the else statement though, I am not able to finish entering text in the second text field before the “Sorry Charlie” message appears.

Please, how can I make the password text field wait until all the info has been entered and listen for the Enter key?

Thank you!

passwordBx.addEventListener(KeyboardEvent.KEY_DOWN, greeting, false, 0, true);

//var anyName:String; Want to be able to enter any name later

function greeting(evt:KeyboardEvent):void {

           

if(userNameBx.text = = "Marta"  &&  passwordBx.text  = = "Chicken" && evt.keyCode = = Keyboard.ENTER) {

           

           

            evt.keyCode == Keyboard.ENTER

            welcomeBx.text = "Welcome " + userNameBx.text;

            return;

           

           

            //}else{

                        //welcomeBx.text = "Sorry Charlie!";

                       

            }

           

}

This topic has been closed for replies.
Correct answer kglad

when your listener function is called it checks those conditions and it's always going to be false when that first key is down.  actuallly, each of the three conditions will be false when the first key is down.

you should be using something like:


passwordBx.addEventListener(KeyboardEvent.KEY_DOWN, greeting, false, 0, true);

//var anyName:String; Want to be able to enter any name later

function greeting(evt:KeyboardEvent):void {

           

if(evt.keyCode = = Keyboard.ENTER) {

            if(userNameBx.text = = "Marta"  &&  passwordBx.text  = = "Chicken"){

           

            welcomeBx.text = "Welcome " + userNameBx.text;

           

           } else{

welcomeBx.text = "Sorry Charlie!";

                       

            }

           

            //}

           

}

1 reply

kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
October 10, 2009

when your listener function is called it checks those conditions and it's always going to be false when that first key is down.  actuallly, each of the three conditions will be false when the first key is down.

you should be using something like:


passwordBx.addEventListener(KeyboardEvent.KEY_DOWN, greeting, false, 0, true);

//var anyName:String; Want to be able to enter any name later

function greeting(evt:KeyboardEvent):void {

           

if(evt.keyCode = = Keyboard.ENTER) {

            if(userNameBx.text = = "Marta"  &&  passwordBx.text  = = "Chicken"){

           

            welcomeBx.text = "Welcome " + userNameBx.text;

           

           } else{

welcomeBx.text = "Sorry Charlie!";

                       

            }

           

            //}

           

}

Participant
October 10, 2009

Thank you kglad! It worked! ( and now the whole house knows.)

kglad
Community Expert
Community Expert
October 10, 2009

you're welcome.