Search code examples
actionscript-3flashadobeflash-cs5flashdevelop

Operations carry over on next random


function roll(){
randomNumber = Math.ceil(Math.random() * range);
randomNumber2 = Math.ceil(Math.random() * range);
randomNumber3 = Math.ceil(Math.random() * range);
dice1_mc.gotoAndStop(randomNumber);
dice2_mc.gotoAndStop(randomNumber2);
dice3_mc.gotoAndStop(randomNumber3);
num1 = int(randomNumber);
num2 = int(randomNumber2);
num3 = int(randomNumber3);
trace(num1);
trace(num2);
trace(num3)
}

      function AddCheck(e:MouseEvent):void {
       ans = num1+num2+num3;
       if (displayText.text == String(ans)){
        //score++;
        trace("Correct");
        trace(ans);
        displayText.text ="";
        score+=1;
        displayScore.text = String(score);
        opsymbol=0;
        RandomizeOperation();
    }else{
        trace("answer is " + ans + "------")
        clearTxt();
        //RandomizeOperation()
    }
    }


    function MultiCheck(e:MouseEvent):void {
    ans = num1*num3;
    if (displayText.text == String(ans)){
        //score++;
        trace("Correct");
        displayText.text ="";
        score+=1;
        displayScore.text = String(score);
        opsymbol=0;
        RandomizeOperation();
    }else{
        trace("answer is " + ans + "------")
        clearTxt();
        //RandomizeOperation()
    }
    }

   function SubCheck(e:MouseEvent):void {
    ans = num1-num2-num3;
    if (displayText.text == String(ans)){
        //score++;
        trace("Correct");
        trace(ans);
        displayText.text ="";
        score+=1;
        opsymbol=0;
        displayScore.text = String(score);
        RandomizeOperation();

    }else{
        trace("answer is " + ans + "------")
        clearTxt();
        //RandomizeOperation()
    }
         }


    function RandomizeOperation(){
    var oprange:uint = 2;
    opsymbol = Math.ceil(Math.random() * oprange);
    //opsymbol = 2;
        //trace(opsymbol);

      if(opsymbol == 1){
    dice2_mc.visible= true;
    trace(opsymbol + " addition");
    enterAns_btn.addEventListener(MouseEvent.CLICK, AddCheck);
    roll();
        }
        if(opsymbol == 2){
    dice2_mc.visible= true;
    trace(opsymbol + " subtraction");
    enterAns_btn.addEventListener(MouseEvent.CLICK, SubCheck);
    roll();
        }

            }

enter image description here

The operation carry over everytime the question changes. I dont know whats wrong.

example first question is 1+2+3 (which is 6) and the next question is Subtraction (3-3-1- RIGHT answer is suppose to be -1) but it add. i cant figure out whats wrong


Solution

  • Nothing is carrying over here. You're just not removing the eventListener for mouse from one type to another, instead you are adding to the already there MouseEvent(s). Two traces at once is a big clue:

    You need inside { } of your if (opsymbol == 2):

    //will remove ANY existing current listeners to some function
    enterAns_btn.removeEventListener(event.type, arguments.callee);
    //adds a new listener to some function for this new Check type
    enterAns_btn.addEventListener(MouseEvent.CLICK, SubCheck); 
    


    Why it happens:

    if (opsymbol == 1)
    { 
        //CHECK ONE: enterAns_btn listens for ADD
        enterAns_btn.addEventListener(MouseEvent.CLICK, AddCheck); 
    }
    
    if (opsymbol == 2)
    { 
        //CHECK TWO: enterAns_btn listens for SUBTRACT
        enterAns_btn.addEventListener(MouseEvent.CLICK, SubCheck); 
    }
    

    Because CHECK TWO happened after CHECK ONE, At this point you're now telling Flash that enterAns_btn must actually do two functions on mouse click.. Essentially it heard this command:

    enterAns_btn.addEventListener(MouseEvent.CLICK, AddCheck); //added by check one
    enterAns_btn.addEventListener(MouseEvent.CLICK, SubCheck); //added by check two
    

    And thats why you get two traces results at once.. One for adding to give you the 7 and the other for subtracting to give the -1.. Hope it helps.