Search code examples
actionscript-3flash-cs3addchild

Variable addChild is not defined?


i have a problem using AS3 - Flash CS3 gives me this Error message: Error #1065: Variable addChild is not defined.

Any ideas what's wrong?

This is my code:

package coa.application{
    import flash.display.SimpleButton;
    import flash.text.TextField;
    import flash.text.TextFieldType;

    public class Tab extends SimpleButton {

        public var menuType:String;

        public function Tab(tabText:String, menuType:String) {
            this.menuType=menuType;
            var mytext:TextField=createTextField(0,0,200,20);
            mytext.text=tabText;
        }
        private function createTextField(x:Number, y:Number, width:Number, height:Number):TextField {
            var result:TextField = new TextField();
            result.x=x;
            result.y=y;
            result.width=width;
            result.height=height;
            addChild(result);
            return result;
        }
    }    
}

Solution

  • It's because SimpleButton does not inherit from DisplayObjectContainer but from InteractiveObject.

    addChild is a method from DisplayObjectContainer. SimpleButton contains 3 displayobject for the 3 states and the hittest object, they are named upState, overState, downState and hitTestState.

    So you should be able to set one of them.

    //addChild(result);
    upState = result;
    

    You could just add a DisplayObjectContainer (like a Sprite) to the states and then add the TextField there instead, in case you want to add more graphics to the states.

    upState = new Sprite();
    upState.addChild(new MyButtonBackground()); //Make this class.
    upState.addChild(result);