A Flash Developer Resource Site

Results 1 to 18 of 18

Thread: hangman game help

  1. #1
    Member
    Join Date
    May 2003
    Location
    stuck to my machine - send help!
    Posts
    42

    hangman game help

    Hi all,

    I am trying to make a very small hangman game (written primarily in AS) using an array of words. I have been successful at: retrieving a random word from the array, dynamically creating little lines for each letter in the array (so user can see how many letters in that particular word), dynamically creating little textboxes - one for each letter in the word (though I am having trouble getting them to line up with the lines), and with the help of a layer51 prototype finding the position in the array that a particular letter is at (e.g. in the word mississippi the letter 'i' appears at the 1, 4, 7, and 10 spots in the array).

    But here I am stuck. I can't figure out:
    1) if the letter input by the user matches a letter in the word then have that letter appear in the appropriate text box (should be able to use array returned by prototype to target appropriate text box right?)
    2) how do you write an if statement for an empty array? if array returned by prototype is empty then the user guessed a letter that isn't in the word. I need to store those in a count variable so I can advance the hangman drawing frame by frame.
    3) Create a new word when the 'play again' button is pressed.

    I know I am asking a lot here so if anyone can help me out on ANY of these or point me in the right direction I'd be ever so grateful!

    I will try to attach the fla (if I can figure it out) in case anyone wants to see what I have done so far.

    If you think I am going the wrong way about all this then say that too! Especially if you have suggestions for alternative functions.

    Peace -
    TS (aka minime!)

  2. #2
    Member
    Join Date
    May 2003
    Location
    stuck to my machine - send help!
    Posts
    42
    Okay, here is the fla!
    Attached Files Attached Files

  3. #3
    Member
    Join Date
    May 2003
    Location
    stuck to my machine - send help!
    Posts
    42
    Argh! Still can't get this . . .

    Can anyone help?

  4. #4
    Senior Member [SH]Doc's Avatar
    Join Date
    May 2002
    Location
    Belgium
    Posts
    149
    I've got a book here, about flash...
    One of the chapters taught how to make a hangman game, if you want I can send you the .fla so you could get some inspiration
    To accomplish great things, we must dream as well as act.
    - Anatole France

  5. #5
    Senior Member jbum's Avatar
    Join Date
    Feb 2004
    Location
    Los Angeles
    Posts
    2,920
    1) if the letter input by the user matches a letter in the word then have that letter appear in the appropriate text box (should be able to use array returned by prototype to target appropriate text box right?)
    Code:
    // this assumes your word is in string 'wordStr' and your letter_boxes are named letter0 thru letterN
    
    function showMatchingLetter(letter)
    {
      for (i = 0; i < wordStr.length; ++i) {
        if (wordStr.charAt[i] == letter) {
            _root["letter"+i].text = letter;
        }
      }  
    }
    2) how do you write an if statement for an empty array? if array returned by prototype is empty then the user guessed a letter that isn't in the word.
    Code:
    if (array == undefined || array.length == 0)
    {
      // array is non-existant or empty
    }
    3) Create a new word when the 'play again' button is pressed.
    Put the code you are currently using to setup your code into a function.

    Call this function once after you define it, to setup your game. Call it again, when the user presses the 'play again' button.

    Code:
    function setupGame()
    {
      // code you are currently using to setup the game...
    }
    
    setupGame(); // call it once to set things up..
    You will probably find that to reset the game, you will need to do a few additional things, such as reset the various arrays you have been using

    myArray = [];

    and clear the contents of your text boxes.

    - Jim

  6. #6
    Member
    Join Date
    May 2003
    Location
    stuck to my machine - send help!
    Posts
    42
    Hooray! An answer! Thanks you guys! I'm gonna try implementing that code right now!

    jbum you are awesome - an excellent coder and full of help, both here AND on kirupa!

    Cheers mate!


  7. #7
    Member
    Join Date
    May 2003
    Location
    stuck to my machine - send help!
    Posts
    42
    Sorry to bug you but I could not get the 1st one to work. How could I adapt it to the fla I posted? I tried:
    code:

    function showMatchingLetter(letter){
    for(i=0;i<guessword.length; i++){
    if(guessword.charAt[i] == letter) {
    _root["textfield"+i].text=letter;
    }
    }
    }



    but nothing showed. I have so much code on the AS layer that I don't even remember what does what anymore! Any chance you could look at the fla and tell me what I need to replace with the above? (and maybe how to call it?) heh heh - thanks so much!

  8. #8
    Senior Member jbum's Avatar
    Join Date
    Feb 2004
    Location
    Los Angeles
    Posts
    2,920
    Working on it... will post in a bit...

  9. #9
    Senior Member jbum's Avatar
    Join Date
    Feb 2004
    Location
    Los Angeles
    Posts
    2,920
    Okay, here's a modified version of your .fla. I like the simplicity of your fla.

    You also might want to consider responding to direct keystrokes, so the user doesn't have to hit the 'guess' button.

    I've simplified your code somewhat, and eliminated the sub-routine that returns the array of correct letter positions - it was making things overly complicated.

    Also implemented your game reset, and game win detection.

    I attached your text fields to your movieclip 'word' (the same one that the dashes are attached to). This way, it's easier to reset the game by deleting the 'word' movieclip (which deletes everything attached to it). By doing so, I had to change the letter coordinates so that they are relative to the position of word.
    Attached Files Attached Files
    Last edited by jbum; 04-19-2004 at 05:18 PM.

  10. #10
    Member
    Join Date
    May 2003
    Location
    stuck to my machine - send help!
    Posts
    42
    Dude, you need to change your name to ActionScript Hero. Thank you SO much. I've been struggling with this for days and kept hitting the same wall no matter what I tried. To be honest I didn't even think I'd get as far as I did!

    If you have a free moment would you mind commenting on the following code?
    code:

    }
    if (nbrSolved == stringlen) {
    trace('the word has been solved! do something cool here!');
    }
    if (nbrCorrect == 0 && status_txt.text.indexOf(guess) == -1) {
    _root.nextFrame();
    }
    if (status_txt.text.indexOf(guess) == -1)
    status_txt.text += guess;
    _root.guess = "";


    It's the only part that I don't fully grasp.

    And thanks for cleaning out the unnecessary code - it was getting messy in there!

    many many MANY thanks!
    eternally grateful -
    TS

  11. #11
    Senior Member jbum's Avatar
    Join Date
    Feb 2004
    Location
    Los Angeles
    Posts
    2,920
    Code:
    if (nbrSolved == stringlen) {
            trace('the word has been solved! do something cool here!');
    }
    Everytime we match a letter, we are incrementing the variable nbrSolved, which was initialized to zero when the game was reset. When the number of solved letters is equal to the length of the word, we know the game is over, and the user has won. This would be a good time to do something cool, such as making the letters dance a jig, and then maybe resetting the game a few seconds later. I'll leave that part to you.

    Code:
     
    if (nbrCorrect == 0 && status_txt.text.indexOf(guess) == -1) {
            _root.nextFrame();
    }
    Okay, I noticed that you were building up your hangman from frame to frame, and probably wanted to skip to the next frame when the user got a letter wrong.

    In the loop where I check for correct letters, I count the number of matches (nbrCorrect). If it's zero, that means the user didn't match anything (nbrCorrect == 0).

    I noticed, while playing with it, that if I typed 'z' twice, it was penalizing me twice and adding 'z' to the list of guessed letters twice. I only want it to penalize me once for the same letter. So here, I check to see if I've already typed a 'z' with this part:

    status_txt.text.indexOf(guess)

    If this returns -1, that means that 'guess' was not found, and I haven't typed guess ('z') before. If guess is found, it is going to return the index of the letter, which will be >= 0.

    So in the case where the guess isn't found, I move on to the next frame, and we see a bit more of the hangman.

    Code:
    if (status_txt.text.indexOf(guess) == -1)
      status_txt.text += guess;
    _root.guess = ""
    Ditto for this part. If I haven't typed the letter before, then add it to the list of letters I've typed.

    Then reset the guess.

    - Jim

  12. #12
    Member
    Join Date
    May 2003
    Location
    stuck to my machine - send help!
    Posts
    42
    Awesome. Thanks so much. I wanted to comment the code so I can remember what it means when I look at it 3 months from now.

    I never ever EVER would have gotten this part
    code:

    if (nbrCorrect == 0 && status_txt.text.indexOf(guess) == -1) {
    _root.nextFrame();
    }


    so thanks for explaining that.

    I fixed a couple of little minor things (messages and clearing the letters guessed when a new game is started) and am just researching a 'wait 5 seconds before restarting game' code so if you'd like I can post the finished file when its done?

    Thanks again man, I really appreciate it.
    TS

  13. #13
    Senior Member jbum's Avatar
    Join Date
    Feb 2004
    Location
    Los Angeles
    Posts
    2,920
    Cool - looking forward to seeing your finished game.

  14. #14
    Member
    Join Date
    May 2003
    Location
    stuck to my machine - send help!
    Posts
    42
    Here it is - I didn't even need the timer! Look for it on Kirupa - its going to be my new footer! With the list of words changed of course.

    Thanks again for all your help!!
    TS
    Attached Files Attached Files

  15. #15
    Junior Member
    Join Date
    Aug 2004
    Posts
    9
    Hi jbum and trialsize,

    I like to use the same code with your permission if I may and develop little further perhaps with your help.

    What I like to do is to have the A-z character set as graphics, Then every time a letter used right or wrong it gets highlighted. I have no experience in AS so your help would be much appreciated.

    Thanks

  16. #16
    Junior Member
    Join Date
    Aug 2004
    Posts
    9
    here is what I mean in a more simple version of course. http://www.inglese.it/impiccato.htm

  17. #17
    Member
    Join Date
    May 2003
    Location
    stuck to my machine - send help!
    Posts
    42
    You are definitely free to use the code. I don't think jbum will mind as he was so helpful when I was stuck. I don't have time at the moment to help you but hope to check it out when I get back from being out of town.


  18. #18
    Junior Member
    Join Date
    Aug 2004
    Posts
    9
    Thanks, We 'll be waiting eagerly

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