Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Buttons on keys

New Here ,
Feb 11, 2013 Feb 11, 2013

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!

TOPICS
ActionScript
973
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Feb 11, 2013 Feb 11, 2013

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

Translate
LEGEND ,
Feb 11, 2013 Feb 11, 2013

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 12, 2013 Feb 12, 2013

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)

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 12, 2013 Feb 12, 2013

guessLetter = String.fromCharCode(evt.charCode)

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 12, 2013 Feb 12, 2013

Is there anyway to convert the letter the user enters to capital? Thanks again.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 12, 2013 Feb 12, 2013

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()

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 13, 2013 Feb 13, 2013

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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 13, 2013 Feb 13, 2013

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 13, 2013 Feb 13, 2013

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 13, 2013 Feb 13, 2013

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"

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 13, 2013 Feb 13, 2013

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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 13, 2013 Feb 13, 2013

Not if you already have one.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 13, 2013 Feb 13, 2013
LATEST

Oh I fixed it nevermind! Thanks for all the help though, really appreciated!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines