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

**URL:** <https://forum.kirupa.com/t/i-didnt-want-to-post-this-here-but-as2-flash-8/261247>\
**Category:** flash\
**Created:** [May 24, 2008, 10:21pm UTC](https://forum.kirupa.com/t/i-didnt-want-to-post-this-here-but-as2-flash-8/261247 "2008-05-24T22:21:47Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Valaran](https://avatars.discourse-cdn.com/v4/letter/v/ee59a6/32.png) [@Valaran](https://forum.kirupa.com/u/Valaran)\
**Post date:** [May 24, 2008, 10:21pm UTC](https://forum.kirupa.com/t/i-didnt-want-to-post-this-here-but-as2-flash-8/261247/1 "2008-05-24T22:21:47Z")

</div>

Hi,

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

```auto

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:

```auto

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? 🙂

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![claudio](https://avatars.discourse-cdn.com/v4/letter/c/7cd45c/32.png) [@claudio](https://forum.kirupa.com/u/claudio)\
**Post date:** [May 25, 2008, 3:51pm UTC](https://forum.kirupa.com/t/i-didnt-want-to-post-this-here-but-as2-flash-8/261247/2 "2008-05-25T15:51:10Z")

</div>

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 🙂

```auto

// 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);

```
