1180:调用可能未定义的方法FlipUntilHead

时间:2013-10-05 18:41:09

标签: actionscript-3

package {
    import flash.display.MovieClip;
    import flash.text.TextField;
    import flash.text.TextFieldType;
    import flash.events.MouseEvent;

    public class coinFlip extends MovieClip {// class
        var bool:Boolean;
        var num1:Number;
        var counter:int;
        public function coinFlip() {// constructor
            RandomTruth();
            var textF1:TextField = new TextField();
            FlipUntilHead(RandomTruth);             // the error is right here*
            if (bool == false) {
                textF1.text = "tails";
            } else {
                textF1.text = "heads";
            }
            addChild(textF1);
            function RandomTruth() {
                num1 = Math.random();
                //trace(num1);

                if (num1 < 0.5) {
                    bool = false;
                    return;
                } else {
                    bool = true;
                    return;
                }
            function FlipUntilHead(RandomTruth) {
                while (bool == false) {
                    RandomTruth();
                    counter ++;
                    return
                    }
                //trace(counter);
                }
            }
        }// end of constructor
    }// end of class
}// end of package (program)

该程序应该“翻转”一枚硬币,直到它使用Math.random和布尔变量落在头上,以声明它的“头部”或“尾部”。

我认为这可能是一个非常简单的错误,我只是愚蠢(编程初学者)。 错误是:

“1180:调用可能未定义的方法FlipUntilHead”

1 个答案:

答案 0 :(得分:0)

问题1:您不必将任何参数传递到FlipUntilHead()函数,因为该函数已调用RandomTruth()

而不是function FlipUntilHead(RandomTruth)使用function FlipUntilHead()

然后,而不是

RandomTruth();
var textF1:TextField = new TextField();
FlipUntilHead(RandomTruth);

使用

var textF1:TextField = new TextField();
FlipUntilHead();

虽然函数名确实存在但是错误被抛出,因为它不理解你将RandomTruth传递给它的方式。

问题2:此外,在}之前应该有function FlipUntilHead(){,在程序结束时应该少}

问题3:最后,while中的FlipUntilHead()循环没有正确循环,因为有一个return语句会立即结束该函数第一个循环。这是最终的代码:

package {
    import flash.display.MovieClip;
    import flash.text.TextField;
    import flash.text.TextFieldType;
    import flash.events.MouseEvent;

    public class coinFlip extends MovieClip {
        var bool:Boolean;
        var num1:Number;
        var counter:int;
        public function coinFlip() {
            var textF1:TextField = new TextField();
            FlipUntilHead();            
            if (bool == false) {
                textF1.text = "tails";
            } else {
                textF1.text = "heads";
            }
            addChild(textF1);
            function RandomTruth() {
                num1 = Math.random();
                if (num1 < 0.5) {
                    bool = false;
                    return;
                } else {
                    bool = true;
                    return;
                }
            }
            function FlipUntilHead() {
                while (bool == false) {
                    RandomTruth();
                    counter++;
                }
            }
        }
    }
}