I didn't want to post this here.. But [AS2, Flash 8]

Hi,

I’ve never used classes in AS2 before, however I thought it’d be worth trying. Anyway, this is the class:


class Unit {
    /********************************
    ** CLASS VARIABLES
    ********************************/
    private var clip:MovieClip;
    
    /********************************
    ** CONSTRUCTOR
    ********************************/
    public function Unit(scope:Object, shape) {
        var n:String = ranGen(20);
        while(scope[n]){
            var n:String = ranGen(20);
        }
        clip = scope.createEmptyMovieClip(n, scope.getNextHighestDepth());
        if(typeof(shape) == "string"){
            clip.attachMovie(shape, shape, clip.getNextHighestDepth());
        } else {
            drawArray(shape);
        }
    }

    /********************************
    ** SET FUNCTIONS
    ********************************/
    // Set currentframe
    public function setFrame(frame:Number):Void {
        trace(clip + ".gotoAndStop( " +frame+ " )");
        clip.gotoAndStop(frame);
    }
    // Set Y pos
    public function setY(y:Number):Void {
        clip._y = y;
    }
    // Set X pos
    public function setX(x:Number):Void {
        clip._x = x;
    }

    /********************************
    ** PRIVATE FUNCTIONS
    ********************************/
    private function drawArray(arr:Array, opt:Object):Void {
        trace(arr);
        (opt)? clip.lineStyle(opt.thick, opt.rgb, opt.alpha) : clip.lineStyle(1, 0x000000, 100);
        clip.moveTo(arr[0].x, arr[0].y);
        for(var i:Number = 1;i<arr.length;i++){
            clip.lineTo(arr*.x, arr*.y);
        }
    }
    private function ran(high:Number, low:Number):Number {
        return Math.floor(Math.random() * (high -  low)) + low;
    }
    private function ranGen(len:Number):String {
        var str:String = "";
        for(var i:Number = 0;i<len;i++){
            str += chr(ran(120, 100));
        }
        return str;
    }
}

And the fla:


var objects:Array = [];
var map:Array = [
                 [1, 1, 1, 1, 1, 1, 1, 1, 1],
                 [1, 0, 0, 0, 0, 0, 0, 0, 1],
                 [1, 0, 0, 0, 0, 0, 0, 0, 1],
                 [1, 0, 0, 0, 0, 0, 0, 0, 1],
                 [1, 0, 0, 0, 0, 0, 0, 0, 1],
                 [1, 0, 0, 0, 0, 0, 0, 0, 1],
                 [1, 0, 0, 0, 0, 0, 0, 0, 1],
                 [1, 0, 0, 0, 0, 0, 0, 0, 1],
                 [1, 1, 1, 1, 1, 1, 1, 1, 1]
                 ];

function compileMap(map:Array):Void {
    for(var i:Number = 0;i<map.length;i++){
        for(var j:Number = 0;j<map*.length;j++){
            var objNum:Number = objects.push(new Unit(_root, "mBlock")) - 1;
            objects[objNum].setFrame(map*[j]);
            objects[objNum].setX(j * 20);
            objects[objNum].setY(i * 20);
        }
    }
}
compileMap(map);

The above produces what I want, 9x9 squares placed in the correct order, however, the following line: “objects[objNum].setFrame(map[j]);*” does not work, it does NOT goto the correct frame (Actually it doesn’t even go to any frame), and I’ve got no idea why ._., anyone care to help me out? :slight_smile:

Thanks in advance.

Two considerations.

  1. MovieClip frames are 1-based, so frame 0 actually means frame 1. Correct that in your multidimensional array.
  2. Since “clip” is a reference to a created movieclip that later on you’ll attach some content, try updating its reference at the moment you do the attaching.

Hope it helps :slight_smile:


// in your class file
public function Unit(scope:Object, shape) {
	var n:String = ranGen(20);
	while (scope[n]) {
		var n:String = ranGen(20);
	}
	clip = scope.createEmptyMovieClip(n, scope.getNextHighestDepth());
	if (typeof (shape) == "string") {
		clip = clip.attachMovie(shape, shape, clip.getNextHighestDepth());
	} else {
		drawArray(shape);
	}
}


//in your fla
var objects:Array = [];

// notice I corrected the indexes to make it 1-based
var map:Array = [
                 [2, 2, 2, 2, 2, 2, 2, 2, 2],
                 [2, 1, 1, 1, 1, 1, 1, 1, 2],
                 [2, 1, 1, 1, 1, 1, 1, 1, 2],
                 [2, 1, 1, 1, 1, 1, 1, 1, 2],
                 [2, 1, 1, 1, 1, 1, 1, 1, 2],
                 [2, 1, 1, 1, 1, 1, 1, 1, 2],
                 [2, 1, 1, 1, 1, 1, 1, 1, 2],
                 [2, 1, 1, 1, 1, 1, 1, 1, 2],
                 [2, 2, 2, 2, 2, 2, 2, 2, 2]
                 ];

function compileMap(map:Array):Void {
    for(var i:Number = 0;i<map.length;i++){
        for(var j:Number = 0;j<map*.length;j++){
			var obj = new Unit(_root, "mBlock");
          	obj.setFrame(map*[j]);
           	obj.setX(j * 20);
            obj.setY(i * 20);
        }
    }
}
compileMap(map);