To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here


A Flash Developer Resource Site

Go Back   Flash Kit Community Forums > Flash Help > Flash ActionScript

Reply
 
Thread Tools Search this Thread Display Modes
Old 04-15-2004, 05:45 PM   #1
trialsize
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!)
trialsize is offline   Reply With Quote
Old 04-15-2004, 05:48 PM   #2
trialsize
Member
 
Join Date: May 2003
Location: stuck to my machine - send help!
Posts: 42
Okay, here is the fla!
Attached Files
File Type: zip hangman.zip (6.4 KB, 1135 views)
trialsize is offline   Reply With Quote
Old 04-19-2004, 03:50 PM   #3
trialsize
Member
 
Join Date: May 2003
Location: stuck to my machine - send help!
Posts: 42
Argh! Still can't get this . . .

Can anyone help?
trialsize is offline   Reply With Quote
Old 04-19-2004, 03:57 PM   #4
[SH]Doc
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
[SH]Doc is offline   Reply With Quote
Old 04-19-2004, 04:22 PM   #5
jbum
Senior Member
 
jbum's Avatar
 
Join Date: Feb 2004
Location: Los Angeles
Posts: 2,920
Quote:
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;
    }
  }  
}
Quote:
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
}
Quote:
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
__________________
jbum is offline   Reply With Quote
Old 04-19-2004, 04:56 PM   #6
trialsize
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!

trialsize is offline   Reply With Quote
Old 04-19-2004, 05:13 PM   #7
trialsize
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!
trialsize is offline   Reply With Quote
Old 04-19-2004, 06:05 PM   #8
jbum
Senior Member
 
jbum's Avatar
 
Join Date: Feb 2004
Location: Los Angeles
Posts: 2,920
Working on it... will post in a bit...
__________________
jbum is offline   Reply With Quote
Old 04-19-2004, 06:16 PM   #9
jbum
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
File Type: fla hangman2.fla (20.0 KB, 576 views)
__________________

Last edited by jbum; 04-19-2004 at 06:18 PM.
jbum is offline   Reply With Quote
Old 04-19-2004, 06:44 PM   #10
trialsize
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
trialsize is offline   Reply With Quote
Old 04-19-2004, 07:18 PM   #11
jbum
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
__________________
jbum is offline   Reply With Quote
Old 04-19-2004, 07:36 PM   #12
trialsize
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
trialsize is offline   Reply With Quote
Old 04-19-2004, 07:38 PM   #13
jbum
Senior Member
 
jbum's Avatar
 
Join Date: Feb 2004
Location: Los Angeles
Posts: 2,920
Cool - looking forward to seeing your finished game.
__________________
jbum is offline   Reply With Quote
Old 04-19-2004, 08:10 PM   #14
trialsize
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
File Type: fla hangman2.fla (37.5 KB, 633 views)
trialsize is offline   Reply With Quote
Old 10-07-2005, 09:26 PM   #15
oavs
Junior Member
 
Join Date: Aug 2004
Posts: 8
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
oavs is offline   Reply With Quote
Old 10-07-2005, 09:39 PM   #16
oavs
Junior Member
 
Join Date: Aug 2004
Posts: 8
here is what I mean in a more simple version of course. http://www.inglese.it/impiccato.htm
oavs is offline   Reply With Quote
Old 10-11-2005, 12:11 AM   #17
trialsize
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.

trialsize is offline   Reply With Quote
Old 10-11-2005, 12:19 AM   #18
oavs
Junior Member
 
Join Date: Aug 2004
Posts: 8
Thanks, We 'll be waiting eagerly
oavs is offline   Reply With Quote
Reply

Go Back   Flash Kit Community Forums > Flash Help > Flash ActionScript

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 02:30 PM.


internet.commerce
Be a Commerce Partner
 »  »  »  »  »  »  »
 »  »  »  »  »  »
 

    

Acceptable Use Policy


The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.