dcsimg
A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: Retrieve word from random array index

  1. #1
    Junior Member
    Join Date
    Jun 2005
    Posts
    8

    Retrieve word from random array index

    Hello, I am trying to build a hangman game in 3dfa & things are going ok but very slow. I have the words coming up randomly when new game button is pushed but only the indexes (integers). How do I turn the index back into the word it represents? The below code works but I can't seem to find any help on changing from integer back to a string. Any help appreciated, Chris.


    //generate the words for game
    game_words = new Array("dog","said","town","word","happy","youth"," health","baker","computer","school","keys","table" ,"family","bike","horse","question")

    //make the word indexes random
    game_words = random(16)


    //show the random indexes in txt4
    txt4 = game_words
    Timespider

  2. #2
    KoolMoves Moderator blanius's Avatar
    Join Date
    Jul 2001
    Location
    Atlanta GA
    Posts
    5,244
    that is not they way.

    The only real tricky part is to not repeat words till they have all been used.

    game_words = new Array("dog","said","town","word","happy","youth"," health","baker","computer","school","keys","table" ,"family","bike","horse","question")
    w=new Array;
    wordscount=game_words.length

    for (idx=0;idx<wordscount;idx++){//loop through array one at a time
    windex=random(wordscount)//get random element
    w[idx]=game_words[windex]//put it in the w array
    trace (game_words[idx]+"->"+w[idx])
    }

    see if that doesn't work for you.

    when you need a new random word you can just loop through the w array one at a time.

  3. #3
    Junior Member
    Join Date
    Jun 2005
    Posts
    8
    Thanks Bret, I thought i was on the right track till I saw your code. I understand it & you have got me over my hurdle. I can see I might have a couple more questions in the future but thanks for now.
    Chris.
    Timespider

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center