actionscriptflash-8

Guess function in Flash


I am stuck with my Flash game. I need the syntax in action script for the below logic. My game is pretty large and complex to explain and I will explain you a quick simple example.

I have 2 buttons. First one is button1 and the Second one is button2. When I click on button1 there should be a guess and response your guess is right or wrong. It's just a random guess function to say my guess is right or wrong. There are only 2 buttons either button1 or button2 should be correct as per instance.

Can anyone tell me the syntax in action script to check the random guess?

Thanks in advance.


Solution

  • If I understood your question correctly this should work:

    button1.addEventListener(MouseEvent.CLICK, randomGuess); // Event Listeners
    button2.addEventListener(MouseEvent.CLICK, randomGuess);
    
    function randomGuess(event:MouseEvent):void
    {
        var guess:Boolean =  Boolean(Math.round(Math.random()));//Random Value
        var input:Boolean; // To define input value
        if (event.target.name == "button1"){
            input = true; // set input value
        }
        else if (event.target.name == "button2"){
            input = false;
        }
    
        if (guess == input){ //compare input value to the random value
            trace("You guessed it.");
        }
        else{
            trace("You didnt guess it.");
        }
    

    }