0
votes

This is my first class and i try to make rectangle with stage sizes, but flash gives me these errors:

addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
// 1180: Call to a possibly undefined method addEventListener.

removeEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
// 1180: Call to a possibly undefined method removeEventListener.

stageW = stage.stageWidth;
// 1120: Access of undefined property stage.

stageH = stage.stageHeight;
// 1120: Access of undefined property stage.

addChild(mc_background);
// 1180: Call to a possibly undefined method addChild.

My code is:

package {
    import flash.display.MovieClip;
    import flash.events.Event;

    public class main {
        var mc_background:MovieClip = new MovieClip();
        var stageW:Number;
        var stageH:Number;

        public function init() {
            addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
        }

        private function addedToStageHandler(evn:Event):void {
            removeEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);

            stageW = stage.stageWidth;
            stageH = stage.stageHeight;

            drawBackground();
        }
        private function drawBackground():void {
            mc_background.beginFill(0xFF00CC);
            mc_background.graphics.drawRect(0,0,stageW,stageH);
            mc_background.graphics.endFill();
            addChild(mc_background);
        }
    }

}
1

1 Answers

2
votes

Your class "main" should extend a Sprite to use the addChild() and removeEventListener() methods.

So you should import the Sprite class and extends your class from Sprite, like so:

package {
    import flash.display.MovieClip;
    import flash.display.Sprite;
    import flash.events.Event;

    public class main extends Sprite

And it's also considered a nice practice to call class names with first capital letter, e.g main > Main. All lower-case are usually variables, so it will confuse you later.