TypeError: Error #2007: Parameter child must be non-null. at flash.display::DisplayObjectContainer/addChild()

元气小坏坏 提交于 2019-12-12 06:52:53

问题


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

I've declared it as a public var and I'm using addChild(submit_button);

  • 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
    

回答1:


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.

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

private function buttShape():void {}

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

This means that your code

submit_button.upState=buttonShape();

is essentially compiling to:

submit_button.upState=null;


来源:https://stackoverflow.com/questions/5533019/typeerror-error-2007-parameter-child-must-be-non-null-at-flash-displaydisp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!