PDA

View Full Version : [AS3] Help with hangman game


Deathwalker666
04-23-2011, 08:33 PM
Hi guys.
Really tearing my hair out with this problem. I've managed to get all the way with this Flash Hangman minigame, then noticed this problem. Everything works fine, but if there's a word with more than 1 of the same letter in it, only the first one of that letter works. For example, if the hangman word is baboon, and the player pressed O, only ___O___, rather than _____OO__. Here is the code I'm using to check the letters:
//Letter has been selected
function selectLetter(event:MouseEvent) {
//Hide the selected letter
event.target.mouseEnabled=false;
event.target.parent.visible=false;
//Guessed word is it's letter
guessLetter=event.target.parent.character.text;
//If the letter is correct
if (hangWord.indexOf(guessLetter)>-1) {
//Access the selected letter, and make it visible
wordHolder.getChildByName("hangmanLetter_"+guessLetter).visible=true;
lettersCorrect+=1;
//If all the letters are correct
if (lettersCorrect==hangWord.length) {
trace("COMPLETE");
}
} else {
lives-=1;
hangman.nextFrame();
wrongX+=20;
//Put an incorrect letter
var theWrong:wrongLetter = new wrongLetter();
theWrong.letter.text=guessLetter.toUpperCase();
theWrong.x=wrongX;
incorrectHolder.addChild(theWrong);
//If all lives are gone
if (lives==0) {
trace("GAME OVER");
}
}
}
Basically, I need to check if the word contains that letter, and if it does find one, carry on checking through the rest of the word.
I know it's not much to go on, but hopefully someone can help :)
Thanks guys

henke37
04-23-2011, 08:42 PM
You need to repeat the checking until there are no more matches. This sounds like a great task for a loop.

Deathwalker666
04-23-2011, 10:44 PM
Yep, thanks mate. I did try that, but having no luck. Here;s the code I used:
//Letter has been selected
function selectLetter(event:MouseEvent) {
//Hide the selected letter
event.target.mouseEnabled=false;
event.target.parent.visible=false;
//Guessed word is it's letter
guessLetter=event.target.parent.character.text;
//If the letter is correct
if (hangWord.indexOf(guessLetter)>-1) {
//Access the selected letter, and make it visible
for (var k=0; k<hangWord.length; k++) {
if(hangWord.charAt(k) == guessLetter) {
trace(hangWord.charAt(k))
wordHolder.getChildByName("hangmanLetter_"+?????.visible=true;
}
//correctLetters
//wordHolder.getChildByName("hangmanLetter_"+guessLetter).visible=true;
lettersCorrect+=1;
}
//If all the letters are correct
if (lettersCorrect==hangWord.length) {
trace("COMPLETE");
}
} else {
lives-=1;
hangman.nextFrame();
wrongX+=20;
//Put an incorrect letter
var theWrong:wrongLetter = new wrongLetter();
theWrong.letter.text=guessLetter.toUpperCase();
theWrong.x=wrongX;
incorrectHolder.addChild(theWrong);
//If all lives are gone
if (lives==0) {
trace("GAME OVER");
}
}
}
Sooo, any ideas? Thanks all!

Deathwalker666
04-24-2011, 09:42 AM
Nevermind all. Started again, and got it working :)

function selectLetter(evt:MouseEvent) {
trace(evt.target.id);
for (var i=0; i<hangmanWord.length; i++) {

if (letters[evt.target.id]==hangmanWord.charAt(i)) {

MovieClip(getChildByName(i)).alpha=1;
}
}
}