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.
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
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
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!
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.
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!
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.
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.
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?
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.
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.