Copy link to clipboard
Copied
I have a hangman game that uses an input text field called 'letterIn_txt' where the user enters a letter and presses a button called 'guess_btn' which the user presses after enter a letter which turns it to capitals and if the guessed letter is equal to the inProgress word, it replaces it with the letter. I was wondering if I'm able to press keys between a-z to preform this making it easier for the user to play the game. It must turn to capital otherwise it wouldn't be seen as equal to the word. Here's how my button works so far.
this.guess_btn.addEventListener(MouseEvent.CLICK, playMe);
function playMe(Event:MouseEvent):void
{
guessLetter = this.letterIn_txt.text.toUpperCase();
for(var i = 0; i< wordLength; i++)
{
if(inProgress.charAt(i) == guessLetter)
{
myDisplay=guessLetter;
correctGuess=true;
rightCount++; }
}
I only want to know how I can press keys to preform this action on screen instead of typing the letter out and pressing the button. Any help would be great, thanks!
You can have a listener for the keyboard and determine which key was pressed.
stage.addEventListener(KeyboardEvent.KEY_UP, checkKey);
function checkKey(evt:KeyboardEvent):void {
trace(String.fromCharCode(evt.charCode))
}
Copy link to clipboard
Copied
You can have a listener for the keyboard and determine which key was pressed.
stage.addEventListener(KeyboardEvent.KEY_UP, checkKey);
function checkKey(evt:KeyboardEvent):void {
trace(String.fromCharCode(evt.charCode))
}
Copy link to clipboard
Copied
Thanks! I'm just having a problem changing myDisplay after pushing a key. How do I name the pushed key 'guessedLetter' so that I can compare it to the in progress word? if(inProgress.charAt(i) == guessLetter)
Copy link to clipboard
Copied
guessLetter = String.fromCharCode(evt.charCode)
Copy link to clipboard
Copied
Is there anyway to convert the letter the user enters to capital? Thanks again.
Copy link to clipboard
Copied
Yes, if you look up the String class you can find all of the properties and methods that support it. In this case you can use the toUpperCase() method...
String.fromCharCode(evt.charCode).toUpperCase()
Copy link to clipboard
Copied
Oh thanks this is all working but I have a little problem if the letter the user presses is false. Before I had a text field called letterIn_txt and if it was false it would do the following:
used+= this.letterIn_txt.text; | |
this.usedLetters_txt.text = used; | |
this.Comp.nextFrame(); |
Making the letter the user entered called 'used' and then adding that wrong letter to the 'usedLetters_txt' field but I'm not sure exactly what I should call it now. I tried calling it the variable Guessed:
Guessed = String.fromCharCode(event.charCode).toUpperCase()
Although it works and moves my Comp movie clip to the next frame, it doesn't add all the incorrect guessed letters to the usedLetters_txt. It would add like, an A, G, Y and that's it. What should I do?
Copy link to clipboard
Copied
It should not be any different than what you did before. You only need to substitute the new way of determining the letter entered for the old.
Copy link to clipboard
Copied
I did try changing it to the new method of determining a letter like so:
var used = String = new String;
if (correctGuess==false)
{
used+= this.Guessed;
this.usedLetters_txt.text = used;
this.Comp.nextFrame();
}
but it only displays the letters A,Y, P and G for some strange reason.
Copy link to clipboard
Copied
Try embedding the font in your textfield.
Also, if you are not doing so, be sure to disable keyboard shortcuts (Control -> Disable Keyboard Shortcuts) in your player when testing in Flash. If you don't, many keys will not work as you wish
If Guessed is in the current timeline, you do not need to use "this." You should avoid unnecessary use oftarget references like "this"
Copy link to clipboard
Copied
I think I'm just applying the Guessed string to too many things and it's causing errors, would it logical to create another string that stores the keys pressed by the user?
Copy link to clipboard
Copied
Not if you already have one.
Copy link to clipboard
Copied
Oh I fixed it nevermind! Thanks for all the help though, really appreciated!
Find more inspiration, events, and resources on the new Adobe Community
Explore Now