Copy link to clipboard
Copied
I am creating a simple guess a number game, I have made all of the basic function to work but I can't get the reset button to work once you have guessed the correct number.
I am getting this error, "Scene 1, Layer 'code', Frame 1, Line 51 1084: Syntax error: expecting rightparen before colon." and "Scene 1, Layer 'code', Frame 1, Line 72 1084: Syntax error: expecting rightparen before colon."
Code:
winGame(event:MouseEvent);
If needed I can post the full code if you wish.
Thanks, Alex
If the errors are fixed you should mark your posting as answered and pursue the next problem in a new posting.
From what I can see in your code, clicking the reset button will not do much of anything. It appears to merely disable/enable buttons, and probably the wrong way. You probably just need to rethink the logic behind your reset scheme.
//Add event listener to reset button
reset_btn.addEventListener(MouseEvent.CLICK, winGame);
function winGame(event:MouseEvent):void{
//disables guest butt
Copy link to clipboard
Copied
You are showing one line of code but two seemingly separate error messages, one for line 51 and the other for line 72. The line you show does not appear to have a problem, but often a syntax error in a line preceding the code flagged in the error is what will cause the compiler to see a later line as being at fault.
If you show lines 1 thru 72 it might be possible to catch where the errors are. Please highlight lines 51 and 72 in the code by bolding them or coloring them differently.
Copy link to clipboard
Copied
Hi, The full code is pasted below.
import flash.events.MouseEvent;
//Set up for global variables.
var beginmessage:String;
var randomNumber:uint;
var my_guess:uint;
var numOfGuesses:uint;
var numOfGuessesMessage:String;
function init() :void {
//Disables the reset button, and enables the guess buton.
reset_btn.enabled =false;
guess_btn.enabled =true;
//Displays a starting message in message_txt.
beginmessage= "Guess a number 1 - 100";
message_txt.text=beginmessage;
//Restrict characters in input_txt field, Only numbers "0-9" can be entered.
input_txt.restrict="0-9";
//Clear text in input field.
input_txt.text="";
//Stores a random number between 1 - 100 to the varible "randomNumber"
randomNumber = Math.floor(Math.random() * 100 + 1);
//Add event listener to guess button
guess_btn.addEventListener(MouseEvent.CLICK, yourGuess);
//Add event listener to reset button
reset_btn.addEventListener(MouseEvent.CLICK, winGame);
}
function yourGuess(event:MouseEvent):void{
//Stores guess data from input field to variable my_guess.
my_guess=uint(input_txt.text);
//Check if guessed number is greater then random number.
if(my_guess > randomNumber){
message_txt.text = "Your guess, " + my_guess + " is too high.";
}
//Check if lower number is lower then random number.
else if (my_guess < randomNumber){
message_txt.text = "Your guess, " + my_guess + " is too low.";
}
//Display message when guess is correct
else{
message_txt.text = "Well done, the number is " + randomNumber + ".";
winGame(event:MouseEvent);
}
}
//Code based on guesses remaining
//Message showing guesses remaining
numOfGuesses = 6;
numOfGuessesMessage = " Guesses remaining";
guess_txt.text = numOfGuesses + numOfGuessesMessage;
guessMessage();
function guessMessage():void {
//Only decrement numofGuess if the input field has characters.
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);
}
}
function winGame(event:MouseEvent):void{
//disables guest button and enables reset button
guess_btn.enabled=false;
reset_btn.enabled=true;
}
function guessAgain(event:MouseEvent):void{
//runs init() method
init();
}
//runs init() method
init();
Copy link to clipboard
Copied
I think I see the problem. You are trying to call the winGame function without a mouseevent and are trying to pass an event that neither exists and is not properly code.
Try changing the code as hown below... (starting at line 51)
winGame();
}
}
//Code based on guesses remaining
//Message showing guesses remaining
numOfGuesses = 6;
numOfGuessesMessage = " Guesses remaining";
guess_txt.text = numOfGuesses + numOfGuessesMessage;
guessMessage();
function guessMessage():void {
//Only decrement numofGuess if the input field has characters.
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();
}
}
function winGame(event:MouseEvent=null):void{
adding the =null to the argiment of the function allows you to call the function without passing the event argument to it.
If you were wanting to pass an event to the function, the proper way to do it is to just pass the variable (event), not the variable type as well (event.MouseEvent), as in winGame(event); Having the ":MouseEvent" in that is the error that was being flagged
Copy link to clipboard
Copied
Thanks for the help, it had fixed the errors but sadly my "play again" button is still not working.
I can't understand why it is not working, When I test the applincation I can't see any errors appearing.
Copy link to clipboard
Copied
If the errors are fixed you should mark your posting as answered and pursue the next problem in a new posting.
From what I can see in your code, clicking the reset button will not do much of anything. It appears to merely disable/enable buttons, and probably the wrong way. You probably just need to rethink the logic behind your reset scheme.
//Add event listener to reset button
reset_btn.addEventListener(MouseEvent.CLICK, winGame);
function winGame(event:MouseEvent):void{
//disables guest button and enables reset button
guess_btn.enabled=false;
reset_btn.enabled=true;
}
If the button had to be clicked to call that function, then it had to be enabled already.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now