用Flash写一个计算题小游戏,循环过程中出错了,请求各位的帮助

游戏的思路:
(1)num1,num2为随机数
(2)运算符号为随机加减号
(3)input中填入数字,提交后判断对错yes或者no输出到output
(4)循环出题,做完十道题后输出over

图片说明
图片说明

出现的错误:
(1)做了几题之后,明明回答正确却输出no。且继续做题时持续输出no.
(2)不到十次就输出over

我的代码:
package
{
import flash.display.MovieClip;
import flash.events.MouseEvent;
public class Main extends MovieClip
{

var number1:uint;
var number2:uint;
var answer:int;
var b:Number;
var count:uint;

    public function Main()
    {
        count=0;
        init();
    }

    function init():void
    {
        number1=Math.ceil(Math.random()*10);
        number2=Math.ceil(Math.random()*10);
        num1.text=""+number1;
        num2.text=""+number2;
        input.text="";
        b=Math.random();
        countGame();

    }

    function countGame():void
    {
        if(count<10)
        {
            count++;
            choseSymbol();

        }
        else
        {
            output.text="over";
            count=0;
        }
    }

    function choseSymbol():void
    {
        if(b>=0.5){
            a.text="+";
            submit.addEventListener(MouseEvent.CLICK,onSubmitClick1);
        }
        else{
            a.text="-";
            submit.addEventListener(MouseEvent.CLICK,onSubmitClick2);
        }
    }

    function onSubmitClick1(event:MouseEvent):void
    {
        answer=int(input.text);
        if(answer==number1+number2)
        {
            output.text="yes";
        }
        else
        {
            output.text="no";

        }
        init();
    }

    function onSubmitClick2(event:MouseEvent):void
    {
        answer=int(input.text);
        if(answer==number1-number2)
        {
            output.text="yes";
        }
        else
        {
            output.text="no";
        }
        init();
    }
}

}

1个回答

package
{
import flash.display.MovieClip;
import flash.events.MouseEvent;
public class Main extends MovieClip
{

    var number1:uint;
    var number2:uint;
    var answer:int;
    var b:Number;
    var count:uint;
    public function Main()
    {
        count = 0;
        init();
    }

    function init():void
    {
        number1 = Math.ceil(Math.random() * 10);
        number2 = Math.ceil(Math.random() * 10);
        num1.text = "" + number1;
        num2.text = "" + number2;
        input.text = "";
        b = Math.random();
        countGame();

    }

    function countGame():void
    {
        if (count<10)
        {
            count++;
            choseSymbol();

        }
        else
        {
            output.text = "over";
            count = 0;
        }
    }

    function choseSymbol():void
    {
        submit.removeEventListener(MouseEvent.CLICK,onSubmitClick2);
        submit.removeEventListener(MouseEvent.CLICK,onSubmitClick1);
        if (b>=0.5)
        {
            a.text = "+";
            submit.addEventListener(MouseEvent.CLICK,onSubmitClick1);
        }
        else
        {
            a.text = "-";
            submit.addEventListener(MouseEvent.CLICK,onSubmitClick2);
        }
    }

    function onSubmitClick1(event:MouseEvent):void
    {
        answer = int(input.text);
        trace(answer,(number1+number2))
        if (answer==(number1+number2))
        {
            output.text = "yes";
        }
        else
        {
            output.text = "no";

        }
        init();
    }

    function onSubmitClick2(event:MouseEvent):void
    {
        answer = int(input.text);
        trace(answer,(number1-number2))
        if (answer==(number1-number2))
        {
            output.text = "yes";
        }
        else
        {
            output.text = "no";
        }
        init();
    }
}

}

你的监听重复监听导致问题,多检测一下问题,trace 排查一下就行

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐