动作脚本错误

Error in ActionScript

我正在使用 ActionScript 3 编写分数简化器,以下代码正在为我查找 gcd。但是,我收到一条错误消息,指出参数计数不匹配。 ArgumentError:错误 #1063:参数计数不匹配。预计2个,得到1个。请指教。谢谢!

 btnDetermine.addEventListener(MouseEvent.CLICK, gcd);

        var no1:Number;
        var no2:Number;

        no1 = Number(txtinno1.text);
        no2 = Number(txtinno2.text);

        function gcd(no1, no2){
            if (no1 == 0 || no2 == 0){
                return 0; 
            }

            if (no1 == no2){
                return no1;
            }



            if (no1 > no2){
                return gcd(no1 - no2, no2);
            }

            else {
                return gcd(no1, no2-no1);
        }
    }

您正在使用两个必需参数(no1no2)定义 gcd 方法:

function gcd(no1, no2)

但是,您还在这一行中将该方法用作鼠标单击处理程序:

 btnDetermine.addEventListener(MouseEvent.CLICK, gcd);

因此,当该点击事件触发时,它会调用 gcd 方法并将 MouseEvent 作为第一个参数传递(仅此而已)。由于 function/method 需要两个参数,因此您会收到错误消息。

我猜你真正想做的是:

//add the click listener to a new function
btnDetermine.addEventListener(MouseEvent.CLICK, clickHandler);

//have the click handler take a single MouseEvent as the only argument
function clickHandler(e:MouseEvent):void {
    //inside this click method, get your numbers.
    var no1:Number = Number(txtinno1.text);
    var no2:Number = Number(txtinno2.text);

    //call the gcd function
    gcd(no1, no2);
}

function gcd(no1:Number, no2:Number):Number {
    if (no1 == 0 || no2 == 0){
        return 0; 
    }

    if (no1 == no2){
        return no1;
    }

    if (no1 > no2){
        return gcd(no1 - no2, no2);
    }
    else {
        return gcd(no1, no2-no1);
    }
}