TypeError:错误#2007:参数child必须为非null。在flash.display :: DisplayObjectContainer / addChild()

时间:2011-04-03 22:41:41

标签: flash actionscript-3 button hyperlink addchild

创建了一个按钮,现在尝试将其添加到屏幕上,即时出现此错误。该按钮的代码是 -

private function submitButton():void {
  submit_button=new SimpleButton();
  submit_button.x=200;
  submit_button.y=200;
  submit_button.upState=buttonShape();
  submit_button.overState=buttonShape();
  submit_button.downState=buttonShape();
}

private function buttonShape() {
  var rectangle:Shape = new Shape; // initializing the variable named rectangle
  rectangle.graphics.beginFill(0x8C2B44); // choosing the colour for the fill, here it is red
  rectangle.graphics.drawRect(200, 200, 300,20); // (x spacing, y spacing, width, height)
  rectangle.graphics.endFill();
}

我已将其声明为公共变量而我正在使用addChild(submit_button);

  • 编辑 - 所有代码都在一个类文件中。我在开始时设置了提交按钮, - private var submit_button:SimpleButton;我有一个函数私有函数submitButton():void {         submit_button = new SimpleButton();         submit_button.x = 200;         submit_button.y = 200;

        submit_button.upState=buttonShape();
        submit_button.overState=buttonShape();
        submit_button.downState=buttonShape();
    
    } and the drawing function seen above. and i add it to the stage in another function  - private function gameOver():void {
    
        addChild(submit_button);
        addChild(gameOver1);      basically this function is called on a hit test. and i have a text box that appears (this works) but when i add the button i get the error
    

1 个答案:

答案 0 :(得分:2)

函数buttonShape()没有返回任何内容。您的初始行var rectangle:Shape = new Shape;正在创建一个局部变量,该变量超出范围并且在函数末尾不再存在。

由于buttonShape()不返回任何内容,因此实际的函数签名为:

private function buttShape():void {}

在函数末尾隐含return void;

这意味着你的代码

submit_button.upState=buttonShape();

基本上是编译为:

submit_button.upState=null;