Search code examples
flashactionscript-3buttonhyperlinkaddchild

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


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
    

Solution

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