Copy link to clipboard
Copied
Hello I have a problem, im fairly new to action script and flash in general but heres my code:
//These are the variables.
var beginMessage:String;
var randomNumber:uint;
var guess:uint;
//It disables the play again button so only the guess button is usable.
playagain_btn.enabled =false;
guess_btn.enabled = true;
//shows a message in the message_txt box.
beginMessage="Choose a number between 1 - 100.";
message_txt.text=beginMessage;
//Basically only allows numbers between 0-9...
input_txt.restrict="0-9";
input_txt.text="";
//This stores a random number between 100 and 1.
randomNumber = Math.floor(Math.random() * 100 + 1);
//Adds an event listener to the guess button.
guess_btn.addEventListener(MouseEvent.CLICK, yourGuess);
function yourGuess(event:MouseEvent):void {
guess=uint(input_txt.text);
//This checks if the guessed number is greater than the random number.
if (guess > randomNumber) {
message_txt.text = "Your guess, " + guess + " is too damn high.";
}
//This checks if the guessed number is lower than the random number.
else if (guess < randomNumber) {
message_txt.text = "Your guess, " + guess + " is too low.";
}
//This displays a message when the correct number is guessed.
else{
message_txt.text = "congratulations ,the number is " + randomNumber + ".";
winGame();
}
}
function winGame():void{
//Disables the guess button and enables the play again button.
guess_btn.enabled = false;
playagain_btn.enabled = true;
//This removes a listener from the yourguess button and adds a
//listener to playagain
guess_btn.removeEventListener(MouseEvent.CLICK, yourGuess);
playagain_btn.addEventListener(MouseEvent.CLICK, guessAgain);
}
function guessAgain(event:MouseEvent):void{
//Runs the init() method.
init();
}
//Runs the init() method.
init();
//this part of the script is for the number
//Declare variables
var numOfGuesses:uint ;
var numOfGuessesMessage:String;
function init():void {
//Displays a message showing the total number of guessing remaining
numOfGuesses = 6;
numOfGuessesMessage = " Guesses remaining";
guess_txt.text = numOfGuesses + numOfGuessesMessage;
}
if(numOfGuesses == 0){
message_txt.text = "The number was " + randomNumber + ".";
guess_txt.text = "you lose!!!!!! D:!!.";
winGame();
}
function yourGuess(event:MouseEvent):void {
guessMessage();
}
function guessMessage():void{
//Only decrement the numOfGuesses if the text field contains characters.
if(input_txt.length > 1 && numOfGuesses != 0){
numOfGuesses--;
guess_txt.text = numOfGuesses + numOfGuessesMessage;
}
}
And my errors are duplicate function definition. theres the code its crying about. ![]()
function yourGuess(event:MouseEvent):void {
guessMessage();
}
what Im doing here is making a number guessing game, everything works but the number of guesses wont go down.
I can see you have at least two functions with the same "yourGuess" name in the code you show. You cannot have multiple functions with the same name. Either change one of the names or join the two into one if they both need to execute for the same mouse interaction.
function yourGuess(event:MouseEvent):void {
guess=uint(input_txt.text);
//This checks if the guessed number is greater than the random number.
if (guess > randomNumber) {
message_txt.text = "Your guess, " + guess + " is too damn high."
Copy link to clipboard
Copied
I can see you have at least two functions with the same "yourGuess" name in the code you show. You cannot have multiple functions with the same name. Either change one of the names or join the two into one if they both need to execute for the same mouse interaction.
function yourGuess(event:MouseEvent):void {
guess=uint(input_txt.text);
//This checks if the guessed number is greater than the random number.
if (guess > randomNumber) {
message_txt.text = "Your guess, " + guess + " is too damn high.";
}
//This checks if the guessed number is lower than the random number.
else if (guess < randomNumber) {
message_txt.text = "Your guess, " + guess + " is too low.";
}
//This displays a message when the correct number is guessed.
else{
message_txt.text = "congratulations ,the number is " + randomNumber + ".";
winGame();
}
}
Copy link to clipboard
Copied
I changed one of their names and it got rid of the error, thank you for that but the number of guesses still wont go down 😕
Copy link to clipboard
Copied
Put a trace in the function where you try to decrease the number of guesses and see if the logic is letting you get in to change it...
function guessMessage():void {
//Only decrement numofGuess if the input field has characters.
trace(input_txt.length, numOfGuesses)
if(input_txt.length > 1 && numOfGuesses != 0){
numOfGuesses--;
guess_txt.text = numOfGuesses + numOfGuessesMessage;
}
//When guesses have ran out, A message will appear saying you can reset.
if(numOfGuesses == 0){
message_txt.text = "The number I was thinking was " + randomNumber + ".";
guess_txt.text = "Press play again to try again";
winGame(event:MouseEvent);
}
}
If you happen to be changing frames where you leave the frame with this code and then come back to it, then you are likely running into this line again....
numOfGuesses = 6;
which resets the number of guesses.
Copy link to clipboard
Copied
the output box displays a 0 6 message, and the guesses still wont go down 😕
thanks for your help so far though, i managed to get rid of tons of errors.
Copy link to clipboard
Copied
You need to evaluate what the trace is telling you. If you don't realize it, your response just indicated that the conditions for decreasing the numOfGuesses are not being satisfied... that 0 is indicating the input_txt.length value is not > 1. So if that is always staying at 0, then you will never get passed that if() to decrease the guess count
Figure out why you get that 0 and you might have this solved (I say might because there could be other problems yet to unfold)
Copy link to clipboard
Copied
Thank you for all your help, I really appreciate it ![]()
unfortuantley the code still didn't work as well as it should have but non the less your help got rid of some anoying errors.
![]()
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more