Hey everyone!
Having a problem with my program - I have a textfield where a user inputs an answer to a question, then presses a button or presses the enter key to submit the answer. Code checks it, if it works good, if not, clear it out and try again.
Problem is, when the answer is submitted using the Enter key and the answer is incorrect, when the TextField is cleared a carriage return is left behind. I can’t figure out a way to handle this situation. Here is the code:
TextField is declared:
var inputBox:TextField = new TextField;
inputBox.type = TextFieldType.INPUT;
inputBox.defaultTextFormat = txtFormatting_Right;
inputBox.x = 10;
inputBox.y = 62;
inputBox.width = 118;
inputBox.height = 25;
inputBox.multiline = true;
inputBox.wordWrap = true;
addChild(inputBox);
Keyboard Event Listener is created:
inputBox.addEventListener(KeyboardEvent.KEY_DOWN, handleKeyboard);
Listener checks for the Enter key and calls next function
function handleKeyboard(key:KeyboardEvent):void {
var keyPressed:String = "";
keyPressed = key.keyCode.toString();
if (keyPressed == "13")
{
answerTest();
}
}
And the final bit of code:
function answerTest(): void {
var answerEntered:String = new String;
answerEntered = inputBox.text;
inputBox.text = "";
trace("|"+answerEntered+"|"+answer+"|");
if (answerEntered == answer) {
mc.alpha = 0;
removeChild(inputBox);
response.htmlText = pos;
} else {
response.htmlText = neg;
}
}
This is what ends up being traced from above:
[COLOR=“DarkRed”]|4|7|
|
5|7|
|
6|7|
|
7|7|
|
8|7|
|
9|7|[/COLOR]
The first trace (|4|7|) is the way I need it to show - without the carriage return between the first pipe and first ‘answerEntered’.
I hope that is enough info. I don’t know how to check for the numbers without the carriage return, perhaps using RegExp (seems like overkill). It works absolutely fine if a person clicks a button off to the side to call the AnswerTest() function.
Thanks,
Andrew