操作继续下一个随机

时间:2014-12-07 12:58:36

标签: actionscript-3 flash adobe flash-cs5 flashdevelop

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

每次问题发生变化时,操作都会延续。我不知道什么是错的。

示例第一个问题是1 + 2 + 3(这是6),下一个问题是减法(3-3-1-正确答案假设为-1)但它添加了。我无法弄清楚什么是错的

1 个答案:

答案 0 :(得分:1)

这里没有任何东西。您只是不将鼠标的eventListener从一种类型移到另一种类型,而是添加已经存在的MouseEvent。两条痕迹同时是一个很大的线索:

您需要{ }内的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); 


为什么会这样:

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); 
}

因为CHECK TWO发生在CHECK ONE之后,此时你现在告诉Flash enterAns_btn 必须在鼠标点击时实际执行两项功能..基本上它听到了这个命令:

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

这就是为什么你一次得到两条痕迹的结果..一个用于添加给你7,另一个用于减去给-1。希望它有帮助。