# Adding children \[addChild\] through Classes

**URL:** https://forum.kirupa.com/t/adding-children-addchild-through-classes/227927
**Category:** Uncategorized
**Created:** [June 3, 2007, 11:13pm UTC](https://forum.kirupa.com/t/adding-children-addchild-through-classes/227927 "2007-06-03T23:13:08Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![efnx](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/efnx/32/3696_2.png) [@efnx](https://forum.kirupa.com/u/efnx)
#### Post date: [June 3, 2007, 11:13pm UTC](https://forum.kirupa.com/t/adding-children-addchild-through-classes/227927/1 "2007-06-03T23:13:08Z")

</div>

Hi, I’m having some problems using addChild() in classes. My problem may be that I just don’t understand how a class returns a displayObject. Here is my code, it is supposed to set up a text field, calculate the frames per second using two EventListeners and then enter it into the text field.

```auto

package org.efnx.fps 
{
    import flash.text.TextField;
    import flash.text.TextFormat;
    import flash.text.TextFieldAutoSize;
    import flash.events.Event;
    import flash.events.TimerEvent;
    import flash.utils.Timer;
    
    public class fpsBox extends TextField
    {
        protected var field:TextField;
        protected var frames;
        protected var format:TextFormat;
        
        
        public function fpsBox(xx:int, yy:int)
        {
            var timer:Timer = new Timer(1000);
            frames = 0;
            format.font = "Verdana";
            format.color = 0x000000;
            format.size = 10;
            field.autoSize = TextFieldAutoSize.RIGHT;
            field.defaultTextFormat = format;
            field.x = xx;
            field.y = yy;
            
            timer.addEventListener(TimerEvent.TIMER, tick);
            field.addEventListener(Event.ENTER_FRAME, everyFrame);
            timer.start();
            addChild(field);
        }
        
        protected function everyFrame(event:Event):void
        {
            frames++;
        }
        
        protected function tick(event:TimerEvent):void
        {
            field.text = frames + " FPS";
            frames = 0;
        }
    }
}

```

Here is the code in my main.fla:

```auto

var fps:fpsBox = new fpsBox(100,0);
addChild(fps);

```

I’m getting this error:  
1180: Call to a possibly undefined method addChild.

Thanks if you can help!
