类型错误:错误#2007:参数孩子必须为非空。在flash.display使用::级DisplayObjectContainer /的addChild()错误、参数、类型、孩子

2023-09-08 15:23:23 作者:欲戴王冠、必承其重

创建了一个按钮,现在试着将它添加到屏幕上即时得到这个错误。在code的按钮 -

Created a button, now tried to add it to the screen and im getting this error. The code for the button is -

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);

编辑 - 所有的code是在一个类文件。我设置在开始的提交按钮, - 私人VAR submit_button:SimpleButton的;我有一个函数私有函数提交按钮():无效{         submit_button =新的SimpleButton();         submit_button.x = 200;         submit_button.y = 200;

edit - all the code is in one class file. i set the submit button at the start, - private var submit_button:SimpleButton; i have a function 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();

} 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

推荐答案

功能 buttonShape()不返回任何东西。你inital行 VAR长方形:形状=新的形状; 正在创建一个局部变量,这超出范围,并停止在函数的末尾存在

The function buttonShape() isn't returning anything. Your inital line var rectangle:Shape = new Shape; is creating a local variable, which goes out of scope and ceases to exist at the end of the function.

由于 buttonShape()不返回任何东西,它的实际功能的签名是:

As buttonShape() doesn't return anything, it's actual function signature is:

private function buttShape():void {}

返回void。被隐含在函数的末尾

with a return void; being implied at the end of the function.

这意味着你的code

This means that your code

submit_button.upState=buttonShape();

基本上编译为:

is essentially compiling to:

submit_button.upState=null;