首页 文章

函数作为构造函数执行,但不作为函数执行

提问于
浏览
0

我在displayandmove.as文件中有以下代码:

package {

    import flash.display.MovieClip;

    public class FigureConstruct extends MovieClip {

        public function displayandmove() {
            this.height = stage.stageHeight/5;
            this.width = stage.stageWidth/5;
        }

    }

}

我在displayandmove.fla的第1帧上有以下内容:

var figure:FigureConstruct = new FigureConstruct();
stage.addChild(figure);
figure.x = stage.stageWidth/2;
figure.y = stage.stageHeight/2;

这些文件位于同一目录中 . 在我的FLA库中,我有我的数字MovieClip,它有一类“FigureConstruct”和基类“flash.display.MovieClip” .

目前上面的代码工作正常,因为我发现如果我执行对象大小代码作为构造 - 使用文件名作为函数名称 - 它的工作原理 .

我原本打算做的是在我的AS文件中将我的函数命名为“sizeFigure()”,然后调用“figure.sizeFigure();”在“stage.addChild(figure);”之后在我的FLA的第1帧 .

这个输出

错误#1006:值不是函数 .

任何人都可以解释我缺少什么来让它作为一个函数而不是作为构造函数执行?

我想也许当我为库对象设置我的类和基类指针时我会搞砸......但不确定 .

PS - 对不起,如果我滥用条款,仍然在我去的时候把它们钉死 .

Edit: Below is the original code that does not seem to work until after I changed the name of my function to the name of the file, making it the class constructor. The above version works, below does not.

displayandmove.as

package {

    import flash.display.MovieClip;

    public class FigureConstruct extends MovieClip {

        public function sizeFigure() {
            this.height = stage.stageHeight/5;
            this.width = stage.stageWidth/5;
        }

    }

}

displayandmove.fla:

var figure:FigureConstruct = new FigureConstruct();
stage.addChild(figure);
figure.sizeFigure();
figure.x = stage.stageWidth/2;
figure.y = stage.stageHeight/2;

2 回答

  • 0

    这是有效的,我把它设置得像你一样 .

    package {
    
            import flash.display.MovieClip;
    
            public class FigureConstruct extends MovieClip {
    
                public function displayandmove():void
                {
                    height = stage.stageHeight/5;
                    width = stage.stageWidth/5;
                }
    
    
                public function sizeFigure():void
                {
                    x = stage.stageWidth/2;
                    y = stage.stageHeight/2;
                }
            }
        }
    
  • 1

    错误#1006:值不是函数

    这个error正在您的代码中的其他位置发生 . 该类是否具有名称 value 的属性(函数get / set)?你是否试图将其作为一种功能进行访问?检查代码 value() 并将其替换为 value .

相关问题