Join 244,306 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 780 people online right now. Registration is fast and FREE... Join Now!
Hello. Im attempting to make a simple hi/low game for a class project, but it doesnt seem to be working. Any tips you could give me on what I'm doing wrong would be greatly appreciated. Here is the code I wrote using the Pseudocode my teacher provided me. The button linkage I created is called "shuriken", if you're wondering.
public class shuriken extends MovieClip { PlayerGuess = answer.text; //variables //declare a variable to hold the computer's number
//constructor public function shuriken() { Initialize(); }
//functions private function Initialize() { addEventListener(MouseEvent.shuriken,WasClicked); PlayerGuess = Math.ceil(Math.random() * 100);//use Math.random to generate a number from //0 to 100 and store it in the variable. }
private function WasClicked(event:MouseEvent):void { MovieClip(root).playerguess.text;//now all your tests if ( MovieClip(root).playerguess.text < PlayerGuess) { MovieClip(root).answer.text = "Too Low"; }//puts the text in quotes in the field. //if the text of the input field is less //than the random number, put // "Your guess is too low" in the output field. if ( MovieClip(root).playerguess.text > PlayerGuess) { MovieClip(root).answer.text = "Too High"; }//else if the text of the input field is greater //than the random number, put //"Your guess is too high" in the output field. if ( MovieClip(root).playerguess.text == PlayerGuess) { MovieClip(root).answer.text = "Right!";//else if the text of the input field is equal //to the random number, put //"You got it right!" in the output field. } } } }
When I attempt to test the game, nothing happens when I enter a guess in the input field and press the button. I dont think the flash file is responding to the code. Is there some step I'm forgetting?
Thanks. Looking at it, Im not so sure it should be a movieclip, since when the button is pressed, the game is supposed to tell you if your number is too high or too low. And as i've said, nothing happens when I click the button.
Also, I've edited my code a bit. I'd appreciate any further help you could give. Thanks.
public class shuriken extends MovieClip { //variables //declare a variable to hold the computer's number var Max = 100; var Min = 1; var RandomNumber = Math.round(Math.random() * (Max-Min))+Min;
//constructor public function shuriken() { Initialize(); }
//functions private function Initialize() { shuriken.addEventListener(MouseEvent.CLICK, WasClicked); //use Math.random to generate a number from //0 to 100 and store it in the variable. RandomNumber = Math.ceil(Math.random() * 100);}
private function WasClicked(event:MouseEvent):void { MovieClip(root).playerguess.text;//now all your tests if ( MovieClip(root).playerguess.text < RandomNumber) { MovieClip(root).answer.text = "Too Low"; }//puts the text in quotes in the field. //if the text of the input field is less //than the random number, put // "Your guess is too low" in the output field. if ( MovieClip(root).playerguess.text > RandomNumber) { MovieClip(root).answer.text = "Too High"; }//else if the text of the input field is greater //than the random number, put //"Your guess is too high" in the output field. if ( MovieClip(root).playerguess.text == RandomNumber) { MovieClip(root).answer.text = "Right!";//else if the text of the input field is equal //to the random number, put //"You got it right!" in the output field. } } } }
Okay, I couldn't figure out how to get the class able to look at the stage and its contents so I just passed everything into the class that was needed. Here is what I came up with:
CODE
package classes{ import flash.display.MovieClip; import flash.events.MouseEvent; import flash.text.TextField; public class HiLow{ var Max:Number = 100; var Min:Number = 1; var RandomNumber:Number = Math.round(Math.random()*(Max-Min))+Min; var btn:MovieClip; var answer:TextField; var playerguess:TextField; var correct:TextField; public function HiLow(mc1:MovieClip, mc2:TextField, mc3:TextField, mc4:TextField){ btn = mc1; answer = mc2; correct = mc3; playerguess = mc4; btn.addEventListener(MouseEvent.CLICK, wasClicked); RandomNumber = Math.ceil(Math.random()*100); correct.text = RandomNumber.toString(); } private function wasClicked(e:MouseEvent):void{ playerguess.text; if(parseInt(playerguess.text) < RandomNumber){ answer.text="Too Low"; } else if(parseInt(playerguess.text) > RandomNumber){ answer.text="Too High"; } else{ answer.text="Correct!"; } } } }
It could be called like so:
CODE
import classes.HiLow;
var testing:HiLow = new HiLow(btn, answer, correct, playerguess);
Basically, btn is a movieclip that is listening for the CLICK event then checks if the correct TextField is the same as the playerguess TextField and outputs the stuff into answer TextField.
Sadly, nothing happens when I press the button still. Is there something I need to do the flash file (perhaps on the stage or layers) to make it work? Like do I make a layer that responds to the script or something along thoe lines?
Hello. Im attempting to make a simple hi/low game for a class project, but it doesnt seem to be working. Any tips you could give me on what I'm doing wrong would be greatly appreciated. Here is the code I wrote using the Pseudocode my teacher provided me. The button linkage I created is called "shuriken", if you're wondering.
public class shuriken extends MovieClip { PlayerGuess = answer.text; //variables //declare a variable to hold the computer's number
//constructor public function shuriken() { Initialize(); }
//functions private function Initialize() { addEventListener(MouseEvent.shuriken,WasClicked); PlayerGuess = Math.ceil(Math.random() * 100);//use Math.random to generate a number from //0 to 100 and store it in the variable. }
private function WasClicked(event:MouseEvent):void { MovieClip(root).playerguess.text;//now all your tests if ( MovieClip(root).playerguess.text < PlayerGuess) { MovieClip(root).answer.text = "Too Low"; }//puts the text in quotes in the field. //if the text of the input field is less //than the random number, put // "Your guess is too low" in the output field. if ( MovieClip(root).playerguess.text > PlayerGuess) { MovieClip(root).answer.text = "Too High"; }//else if the text of the input field is greater //than the random number, put //"Your guess is too high" in the output field. if ( MovieClip(root).playerguess.text == PlayerGuess) { MovieClip(root).answer.text = "Right!";//else if the text of the input field is equal //to the random number, put //"You got it right!" in the output field. } } } }
change this
CODE
//functions private function Initialize() { addEventListener(MouseEvent.shuriken,WasClicked); PlayerGuess = Math.ceil(Math.random() * 100);//use Math.random to generate a number from //0 to 100 and store it in the variable. }
to this
CODE
//functions private function Initialize() { shuriken.addEventListener(MouseEvent.CLICK, WasClicked); PlayerGuess = Math.ceil(Math.random() * 100);//use Math.random to generate a number from //0 to 100 and store it in the variable. }
ahh sorry, i only loaded the first post.. didn't see the other replies.. disregard my previous post.