1
votes

Hi i made a custom class where i would like to create x instances of a movieclip. But the following doesn't work:

package {

import flash.display.MovieClip;
public class CustomClass extends MovieClip {

    public function CustomClass(amount:uint) {  
        var Collector:Array = new Array(); 

        //Add and position Tiles to stage.
        for (var i:uint = 1; i <= amount; i++){ 
            var newMovieClip:MovieClip = new MovieClip;
            newMovieClip.y = amount * 10;
            Collector.push(newMovieClip); 
        }
        addChild(Collector);
    }
}

}

I would like to position them on the timeline with

var customClass_mc:CustomClass = new CustomClass(10);
addChild(customClass_mc);

//try to trace the x position of one of the instances. 
trace(customClass_mc.Collector[5].x);

I keep getting the error: Scene 1, Layer 'Layer 1', Frame 1, Line 5 1119: Access of possibly undefined property Collector through a reference with static type CustomClass.

3

3 Answers

2
votes

Firstly, you need to declare Collector as public:

public var Collector:Array = new Array(); 

Your Collector is an array, not a display object, and so it can't be added to the display tree. Instead you would push each newMovieClip onto the display of Custom class and position them inside your for loop. Then you don't need the collector at all, because you can target the movieclips using getChildAt():

trace(customClass_mc.getChildAt(5).x);
1
votes

I found another answer myself which i think is even better!

You don't need the container at all.

when you use the following

package {

    import flash.display.MovieClip;
    public class CustomClass extends MovieClip {

        public function CustomClass(amount:uint) {  

            //Add and position Tiles to stage.
            for (var i:uint = 1; i <= amount; i++){ 
                var newMovieClip:MovieClip = new MovieClip;
                newMovieClip.y = amount * 10;
                newMovieClip.name = "clip"+i;
                addChild(newMovieClip); 
            }
        }
    }
}

No i can acces the movieclips by using:

var customClass_mc:CustomClass = new CustomClass(10);
addChild(customClass_mc);

//try to trace the x position of the fifth instance. 
trace(customClass_mc.getChildByName("child5").y);
0
votes

The variable 'Collector' is only available inside the constructor the way you have it. Collector has to be made public to be accessible from outside the timeline. The best thing to do would be to make a public getter method to access this. So something like:

import flash.display.MovieClip;
public class CustomClass extends MovieClip {

    private var Collector:Array = new Array(); 

    public function get Collector():Array
    {
        return Collector;
    }

    public function CustomClass(amount:uint) {  

        //Add and position Tiles to stage.
        for (var i:uint = 1; i <= amount; i++){ 
            var newMovieClip:MovieClip = new MovieClip;
            newMovieClip.y = amount * 10;
            Collector.push(newMovieClip); 
        }
        addChild(Collector);
    }
}