Copy link to clipboard
Copied
Hey, I'm following the steps to making a random number guessing game, but I can't seem to get my code to work. I am using dynamic and input texts to create an input and output for my game.
What the problem has been is that my guess button doesn't work. I'm not getting any error messages, but it won't output the text that changes based on my if/else statement. I'm not really sure whats wrong with it.
package {
import flash.display.MovieClip;
import flash.events.MouseEvent;
public class Main extends MovieClip {
var startMessage:String;
var mysteryNumber:uint;
var currentGuess:uint;
public function Main() {
init()
guessButton.addEventListener(MouseEvent.CLICK, onGuessButtonClick);
}
function init():void{
startMessage = "I am thinking of a number";
mysteryNumber = 50
currentGuess = uint(input.text);
output.text = startMessage;
input.text = "";
input.backgroundColor = 0xFFCCCCCC;
input.restrict = "0-9";
stage.focus = input;
}
function onGuessButtonClick(event:MouseEvent){
if(currentGuess > mysteryNumber){
startMessage = "too high!";
}
else if (currentGuess < mysteryNumber){
startMessage = "too low!";
}
else {
startMessage = "you win!";
}
}
}
}
1 Correct answer
The first thing to always try is to embed the font in the textfield. Another thing to check is that the textfield textcolor is not the same as the backbround color.
You should put traces in your onGuessButtonClick function so that you can confirm the conditionals are processing as expected.
Copy link to clipboard
Copied
The first thing to always try is to embed the font in the textfield. Another thing to check is that the textfield textcolor is not the same as the backbround color.
You should put traces in your onGuessButtonClick function so that you can confirm the conditionals are processing as expected.
Copy link to clipboard
Copied
It wasn't the background color. This time it was a mix of a couple issues. I moved the currentGuess variable into the onClick function, as well as changing all startMessages is the if/elses to just output.text = "" and it finally worked out. Thanks though, all the help and tips are really making my life easier!
Copy link to clipboard
Copied
You're welcome

