PDA

View Full Version : Input text fields & ENTER key press?


taz2005
10-06-2005, 07:10 PM
Hi everyone!
This is my first post in this forum so bear with me if I am not very clear.

I have a game which has 12 input text fields and a button for each text field. The user types his answer in a text field and then clicks on the button to check if its right or wrong. if correct the score is incremented by 1 and when score is 12 it jumps to end frame.
It all works fine for now.
But I also want to make it work with ENTER key press. User should be able to check his answer by pressing ENTER key instead of clicking the button everytime.

For now all the code is on first frame.
e.g this is the code for the first button


button1.onRelease = function(){
if(inputName1 == "xyz")//inputName1 is the var in the first input text field.
_total = _total +1;
if(_total>=12){
gotoAndStop("end");
}
display.text= ("You got "+_total+" correct.");
}else{
display.text="try again"
}



this goes on for all the 12 buttons.

My question is how to modify it so that even ENTER key press triggers the same actions as the button does.
I have tried to add
on ( keyPress "<Enter>") {
//check answer
}
it works when i have only 1 text field but as soon as i start putting more text fields everything gets messed up and text fields respond wierdly to ENTER key press. I feel there should be some kind of setFocus() commands to make only one input field respond to a key press. But I do not know how to do it.

I am a beginner and need your help.

Thanks a lot.

[Sx]
10-06-2005, 07:35 PM
I would create a centralized function for checking the answers and then just applying calls to it to each field and button... e.g.:

_total = 0;

function checkAnswer(q:Number, a:String):Void {
switch(q) {
case 0:
if(a == "answer on question 1") _total++;
break;
case 1:
if(a == "answer on question 2") _total++;
break;
case 2:
if(a == "answer on question 3") _total++;
break;
// and so on
}
display.text= ("You got "+_total+" correct.");
if(_total>=12) gotoAndStop("end");
}

Then you need to setup actions for your fields to respond on enter, the easiest way is to do it via loop. e.g.:

inputFields = [_root.inputFieldInstance1, _root.inputFieldInstance2, _root.inputFieldInstance3]; // etc.
for(i=0; i<inputFields.length; i++) {
inputFields[i]._kl = new Object();
inputFields[i]._kl.qNo = i;
inputFields[i]._kl.field = inputFields[i];
inputFields[i]._kl.onKeyDown = function() {
if(Key.isDown(Key.ENTER)) _root.checkAnswer(this.qNo, this.field.text);
};
inputFields[i].onSetFocus = function() {
Key.removeListener(this._kl); // just in case it didn't remove it before due to some error
Key.addListener(this._kl);
};
inputFields[i].onKillFocus = function { Key.removeListener(this._kl) };
}

And all you have left is to change script on your buttons to something like:

buttonsList = [_root.button1, _root.button2, _root.button3]; // etc.
for(i=0; i<buttonsList.length; i++) {
buttonsList[i].onRelease = function(){ _root.checkAnswer(i, inputFields[i]); };
}

Just one of possible solutions that can be more tuned, but this is just an example...

hope it helps...

taz2005
10-06-2005, 08:02 PM
Thanka a lot!
That was a real quick reply.
I will try it out and let you know.
I must admit few things are still beyond me like in the code

function checkAnswer(q:Number, a:String):Void {
switch(q) {
//
}

I don't understand what does :Void do and what is switch(q)?

Also one more doubt ..in the code
inputFields[i]._kl = new Object();

what is _kl? is this a property i am not aware of?

Sorry for bothering you with all these questions. Since I will be using your code I might as well understand it.

I am just learning actionscript by myself so these stupid questions will pop-up.
Bear with me please.

Thanks a bunch again.

[Sx]
10-06-2005, 08:24 PM
:Void - says that function doesn't return anything. Not necessary, but a good coding practice. AS2.0 stuff... Tho it's idiotically implemented, and I don't expect it will get any better now when Adobe is calling the shots in MM...

For switch() function, check AS Dictionary.

As for _kl, it's not a property, it's just a temporary variable... You can name it however you like... I usually key listeners call as _kl, but you choose your own coding style.

taz2005
10-06-2005, 08:40 PM
Thanks a lot for clearing my doubts.
I just checked switch() function in AS dictionary and understood its use.
thanks I learnt a new thing today. I will be using it at other places too.

I will now try to make the game properly and let you know when it works.

thanks!!!!!!!!!!!

taz2005
10-11-2005, 08:34 PM
Hi,
Sorry for getting back so late.
But I have been trying to implement the code you gave me for making ENTER KEY press work on input text fields. I have been unsuccessful so far:-(

Somehow the score refuses to add-up. I have also added a sound just to check if an answer is correct in the Switch code where we check if(a=="answer") but it doesn't play. Which makes me feel that Switch command is not doing what it is supposed to do.
Also when I added Void as told by you i keep getting error messages in output window.. unxpected "{". So for now I removed Void and the error messages are gone.

I am adding a zippped fla and in the file on a frame labeled "init1" it has all the original code that i had initially for buttons only and frame labeled "init2" has your code which I tried to customise to my instances.
I have added comments for you in the .fla file which will help you understand what I am saying. Please please let me know what I am doing wrong here. I have Flash MX if it makes any difference.

I would be really obliged if you can have a look and rescue me out of this situation.
thanks

Artech
10-11-2005, 08:57 PM
I went through and changed the FLA so the text1 box will work. LOOK AT THE CODE ON THE SUBMIT 1 BUTTON. That is where I added it along with some other comments, if you have any questions let me know or feel free to email me.

What it is doing is using a centralized check, just like Sx was talking about and using the Selection.getFocus() command to check which box is currently targeted.

NOTE: I didn't do everything for you or you would learn nothing, I did the first box worth and told you how to do the rest.

Brian

PS. My email address is in there if you have any further questions or need any additional help :) .

taz2005
10-11-2005, 10:20 PM
Thanks a lot for your help.

I added the code to more buttons and they work perfectly fine.

My only concern is that this works only if we move in a particular order from text1 to text2-3-4 etc. Like if i fill the text1 box first then text2 box works fine but if I try to fill in any other box before text1 then the key press doesn't work.

This is what I wanted that one should be able to fill in any box first and check answer by pressing key and then move onto whichever he/she wants.

Since you encouraged me to question you so I thought why not ask you this :)

Your help is much appreciated.
thanks

taz2005
10-11-2005, 10:36 PM
Sorry I replied in a hurry and didn't check properly.
I added code in elseif statements for other text boxes and it does work.
So essentially each button should have this getFocus commands with elseif statements for all 12 text boxes.
And then this should be duplicated for each of the button.
I'll try for the rest of them now.

Thanks a lot.

Artech
10-12-2005, 03:47 PM
I'm glad I was able to help :)

Brian

antago
05-29-2008, 12:36 AM
Thanks so much you gys! This ha shelped me, considering actionscript's "enter" listener is a total flopping piece of a shit and simply doesn't work. I got the enter listener to work; God, these listeners are overly complex
I don't understand why some of Flash's stuff is really crappy (like the REALLY basic stuff) while the extraordinary stuff is really well made and easy to use. FOr isntance you'd think flash could at least have a working setfocus function for when the application starts; there appears to be a setfocus and a setdrunkfocus...while setfocus = setdrunkfocus