School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 300,493 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,831 people online right now. Registration is fast and FREE... Join Now!




Trouble making a Hi/Low Game

 

Trouble making a Hi/Low Game

Reigun

3 Jan, 2009 - 08:56 AM
Post #1

New D.I.C Head
*

Joined: 2 Jan, 2009
Posts: 5

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.

CODE
package {
    //imports
    import flash.display.MovieClip;
    import flash.events.MouseEvent;

    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.
            }
        }
    }
}


User is offlineProfile CardPM
+Quote Post


Martyr2

RE: Trouble Making A Hi/Low Game

3 Jan, 2009 - 09:57 AM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 7,246



Thanked: 820 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
What about it is not working? What is it not doing or what errors are you getting from this?

Make sure that your if statements are comparing correctly. You may need to convert text to an integer to compare it with PlayerGuess.

So let us know how it is not working and we can provide further assistance. smile.gif
User is offlineProfile CardPM
+Quote Post

Reigun

RE: Trouble Making A Hi/Low Game

3 Jan, 2009 - 01:44 PM
Post #3

New D.I.C Head
*

Joined: 2 Jan, 2009
Posts: 5

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?
User is offlineProfile CardPM
+Quote Post

BetaWar

RE: Trouble Making A Hi/Low Game

3 Jan, 2009 - 02:43 PM
Post #4

#include <soul.h>
Group Icon

Joined: 7 Sep, 2006
Posts: 4,729



Thanked: 269 times
Dream Kudos: 1400
My Contributions
Well, you are adding an event listener wrong:
CODE
addEventListener(MouseEvent.shuriken,WasClicked);


It should look like so:
CODE
MOVIECLIP.addEventListener(EVENT, FUNCTION_TO_CALL);


So, I think (not sure what is a movieclip in your code) you should try this:
CODE
shuriken.addEventListener(MouseEvent.CLICK, WasClicked);


I just took a quick look, so that may not be the only thing wrong, just the first thing I noticed.

Hope that helps.
User is offlineProfile CardPM
+Quote Post

Reigun

RE: Trouble Making A Hi/Low Game

3 Jan, 2009 - 03:01 PM
Post #5

New D.I.C Head
*

Joined: 2 Jan, 2009
Posts: 5

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.

CODE
package {
    //imports
    import flash.display.MovieClip;
    import flash.events.MouseEvent;

    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.
            }
        }
    }
}

User is offlineProfile CardPM
+Quote Post

BetaWar

RE: Trouble Making A Hi/Low Game

3 Jan, 2009 - 06:42 PM
Post #6

#include <soul.h>
Group Icon

Joined: 7 Sep, 2006
Posts: 4,729



Thanked: 269 times
Dream Kudos: 1400
My Contributions
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.

Hope that helps.
User is offlineProfile CardPM
+Quote Post

Reigun

RE: Trouble Making A Hi/Low Game

3 Jan, 2009 - 09:26 PM
Post #7

New D.I.C Head
*

Joined: 2 Jan, 2009
Posts: 5

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?
User is offlineProfile CardPM
+Quote Post

Reigun

RE: Trouble Making A Hi/Low Game

4 Jan, 2009 - 06:16 AM
Post #8

New D.I.C Head
*

Joined: 2 Jan, 2009
Posts: 5

Edit: Nvm. It works perfectly now. Thanks for all your help.

This post has been edited by Reigun: 4 Jan, 2009 - 09:08 AM
User is offlineProfile CardPM
+Quote Post

BetaWar

RE: Trouble Making A Hi/Low Game

4 Jan, 2009 - 09:16 AM
Post #9

#include <soul.h>
Group Icon

Joined: 7 Sep, 2006
Posts: 4,729



Thanked: 269 times
Dream Kudos: 1400
My Contributions
Add this line of code up with your other imports:

CODE
import flash.display.SimpleButton;


That should work.
User is offlineProfile CardPM
+Quote Post

theRemix

RE: Trouble Making A Hi/Low Game

4 Jan, 2009 - 01:05 PM
Post #10

D.I.C Regular
***

Joined: 19 Oct, 2005
Posts: 426



Thanked: 10 times
My Contributions
QUOTE(Reigun @ 3 Jan, 2009 - 08:56 AM) *

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.

CODE
package {
    //imports
    import flash.display.MovieClip;
    import flash.events.MouseEvent;

    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.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/8/09 04:37AM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month