# How do you make an array 'spawn' a movie clip?

**URL:** <https://forum.kirupa.com/t/how-do-you-make-an-array-spawn-a-movie-clip/322206>\
**Category:** flash\
**Created:** [May 14, 2011, 2:38am UTC](https://forum.kirupa.com/t/how-do-you-make-an-array-spawn-a-movie-clip/322206 "2011-05-14T02:38:39Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![NoobSurfer](https://avatars.discourse-cdn.com/v4/letter/n/e19adc/32.png) [@NoobSurfer](https://forum.kirupa.com/u/NoobSurfer)\
**Post date:** [May 14, 2011, 2:38am UTC](https://forum.kirupa.com/t/how-do-you-make-an-array-spawn-a-movie-clip/322206/1 "2011-05-14T02:38:39Z")

</div>

Im asking this because right now im gettin some code off a tutorial named “How to Create a Platform Game in AS3” that creates the levels with arrays and also creates the sprites with code. The code for this is below

```auto
//LEVEL VARIABLES
//current row that we are creating
var row:int = 0;
//the current lvl
var lvlCurrent:int = 1;
/*The key for the level arrays:
1: Regular Block
X: Main Character
*/
//this variable will hold the character
var X:String = 'MAIN';
//the array for level 1
var lvlArray1:Array = new Array(
    1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
    1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
    1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
    1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
    1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
    1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
    1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
    1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
    1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
    1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
    1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
    1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
    1,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,1,
    1,0,0,0,X,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
    1,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
    1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
);
//creating the level
//this guy will hold all of the blocks
var blockHolder:Sprite = new Sprite();
//then we add him to stage
addChild(blockHolder);
function createLvl():void{
    //getting the current level that we are on
    var lvlArray:Array = MovieClip(root)['lvlArray'+lvlCurrent];
    //we have to find how far this level goes
    //this will be used so we know when to move to the next row
    //there will always be 16 rows, so this is how we find it out
    //of course, this will make the lvl formatting very strict
    var lvlColumns:int = Math.ceil(lvlArray.length/16);
    //now we must create the level
    for(var i:int = 0;i<lvlArray.length;i++){
        if(lvlArray* == 1){
            //checking if we move onto the next row
            //this checks if i is divisible by the # of columns
            if(i/lvlColumns == int(i/lvlColumns)){
                row ++;
            }
            
            //making a new block
            var newBlock:Block = new Block();
            //drawing the block
            newBlock.graphics.beginFill(0xFFFFFF/*The color for shape*/,1/*The alpha for the shape*/);
            //turning the shape into a square
            newBlock.graphics.drawRect(0,0,25,25);
            //change the coordinates of the block
            newBlock.x = (i-(row-1)*lvlColumns)*newBlock.width;
            newBlock.y = (row-1)*newBlock.height;
            //then finally adding it to stage
            blockHolder.addChild(newBlock);
        } else if (lvlArray* == 'MAIN'){
            mcMain.x = (i-(row-1)*lvlColumns)*newBlock.width;
            mcMain.y = (row-1)*newBlock.height;
        }
    }
    //reset the row for another use
    row = 0;
}

//running the createlvl funciton
createLvl();

```

There is an external piece of code named Block that goes with it.

```auto
package{
    import flash.display.Sprite;
    import flash.display.MovieClip;
    import flash.events.*;
 
    //sprites are just movieclips without any frames in them
    public class Block extends Sprite{
        //_root will signify the root of the document
        private var _root:Object;
 
        public function Block(){
            //this code will only be run once
            addEventListener(Event.ADDED, beginClass);
            //this code will constantly be run
            addEventListener(Event.ENTER_FRAME, eFrame);
        }
 
        private function beginClass(event:Event):void{
            //defining the root of the document
            _root = MovieClip(root);
        }
 
        private function eFrame(event:Event):void{
 
        }
    }
}

```

What i want is to change the code to make the array spawn movie clips instead of sprites the code has drawn for me. I think i have to change this piece…

```auto
//making a new block
            var newBlock:Block = new Block();
            //drawing the block
            newBlock.graphics.beginFill(0xFFFFFF/*The color for shape*/,1/*The alpha for the shape*/);
            //turning the shape into a square
            newBlock.graphics.drawRect(0,0,25,25);
            //change the coordinates of the block
            newBlock.x = (i-(row-1)*lvlColumns)*newBlock.width;
            newBlock.y = (row-1)*newBlock.height;
            //then finally adding it to stage
            blockHolder.addChild(newBlock);
        } else if (lvlArray* == 'MAIN'){
            mcMain.x = (i-(row-1)*lvlColumns)*newBlock.width;
            mcMain.y = (row-1)*newBlock.height;

```

… but im not exactly sure :-/

Any help with this would be GREATLY appreciated :ne:
