1
votes

How can I add the MovieClip I have put into the array to the Stage? The following code is a separated .as file and located at the same level with the main.fla I have tried many times but I got the error message -

"ReferenceError: Error #1065: Variable stage is not defined. at Set1() at main_fla::MainTimeline/frame1()"

How can I do? Thank for any help!!

package 
{
    import flash.display.MovieClip;
    import flash.display.Stage;
    public class Set1
    {
        private var map:Array=new Array();

        public function Set1()
        {
            for (var i:Number=0; i<5; i++)
            {
                var cell_mc=new cell();
                cell_mc.x = 50+ i*cell_mc.width;
                cell_mc.y = 50;
                cell_mc.className=i;
                map[i] = cell_mc;
                trace(map[i].className);
                stage.addChild(map[i]);
                }

        }

    }

}
2

2 Answers

2
votes

You are a little mixed up. stage is not a magic variable, instead it is a property that is inherited from the DisplayObject base class. That property gets set internally when a display object is added to the stage. So in your case your class needs to either inherit from a DisplayObject– probably Sprite class. Or simply inject a reference to the Stage from the outside when you invoke your function

0
votes

First you need to set the Main flash file class.You'll do that by clicking on stage in your fla. file and edit you class in properties(should look like this(class:Set1)) Code below should work fine

 package 
    {
        import flash.display.MovieClip;
        import flash.display.Sprite;
        public class Set1 extends Sprite
        {
            private var map:Array=new Array();

            public function Set1()
            {
                for (var i:Number=0; i<5; i++)
                {
                    var cell_mc=new cell();
                    cell_mc.x = 50+ i*cell_mc.width;
                    cell_mc.y = 50;
                    cell_mc.className=i;
                    map[i] = cell_mc;
                    trace(map[i].className);
                    addChild(map[i]);
                    }

            }

        }

    }