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

How generate random letters?

Explorer ,
Feb 15, 2010 Feb 15, 2010

I want generate random letter on the stage. Look to this letters and enter this letter on my keyboard.

How i can generate it?

And i try to generate numbers, but i always see wrong message. Why?

var myRandomNumber:Number = Math.ceil(Math.random() * 5);
my_txt.text=String(myRandomNumber);

my_keyboardText.text="Key Pressed:";

stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
function keyPressed(event:KeyboardEvent)
{
     my_keyboardText.text="Key Pressed: "+String.fromCharCode(event.charCode);
     if (event.charCode==myRandomNumber)
     {
          my_txt.text="Correct";
     } else
     {
          my_txt.text="Wrong";
     }
}

Thank you.

TOPICS
ActionScript
1.3K
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 15, 2010 Feb 15, 2010

I am not sure what you are trying to do, but you should learn to use the trace command so that you can see what things are and gain understanding.  Example:

function keyPressed(event:KeyboardEvent)
{
     my_keyboardText.text="Key Pressed: "+String.fromCharCode(event.charCode);

    trace(event.charCode,myRandomNumber);
     if (event.charCode==myRandomNumber)
     {
          my_txt.text="Correct";
     } else
     {
          my_txt.text="Wrong";
     }
}

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
Explorer ,
Feb 15, 2010 Feb 15, 2010

yes, i'm tired:))

i was wrong.

i want make something like game 'learn to type in  the blind'.

so user will see letters on the monitor.

and try to repead, usign keyboard.

if user enter correct letter all will be ok:)

so, i dont know how generate random letters and how mach this letters with pressed keys?

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 15, 2010 Feb 15, 2010

Here's something you might be able to build upon...

var letters:Array = new Array("a","b","c","d","e");

var myRandomNumber:Number = Math.floor(Math.random() * 5);

my_txt.text= letters[myRandomNumber];

my_keyboardText.text="Key Pressed:";

stage.addEventListener(KeyboardEvent.KEY_UP, keyUsed);

function keyUsed(event:KeyboardEvent)
{
     var str:String = my_txt.text;
 
  my_keyboardText.text="Key Pressed: "+String.fromCharCode(event.charCode);
   
if (event.charCode == str.charCodeAt(0))
     {
          my_txt.text="Correct";
     } else
     {
          my_txt.text="Wrong";
     }
}

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
Explorer ,
Feb 16, 2010 Feb 16, 2010

my_txt.text= letters[myRandomNumber];

Thank  you, this line was new for me..

you close my eyes:)

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 16, 2010 Feb 16, 2010
LATEST

You're welcome

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
Advocate ,
Feb 15, 2010 Feb 15, 2010

Looking at your question, the answer should be:

var nUpperCase:Number = 65;

var nLowerCase:Number = 97;

var nAscii:Number = nLowerCase + int(Math.random()*25);

var sChar:String = String.fromCharCode(nAscii);

This will generate a random lower case letter. If you want capital letters, use nUpperCase in the code instead of nLowerCase

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