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.
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:
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!