# Adding Childs into a continer, depth

**URL:** https://forum.kirupa.com/t/adding-childs-into-a-continer-depth/327202
**Category:** Uncategorized
**Created:** [February 12, 2012, 8:59pm UTC](https://forum.kirupa.com/t/adding-childs-into-a-continer-depth/327202 "2012-02-12T20:59:40Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![ChrisW182](https://avatars.discourse-cdn.com/v4/letter/c/58956e/32.png) [@ChrisW182](https://forum.kirupa.com/u/ChrisW182)
#### Post date: [February 12, 2012, 8:59pm UTC](https://forum.kirupa.com/t/adding-childs-into-a-continer-depth/327202/1 "2012-02-12T20:59:40Z")

</div>

Hi guys, im having problems with adding children to the stage, Im trying to add two movieclips into a container, then add the container to the stage. The problem is my coins were not displaying (Guessing they were behind my ‘map’) so after doing a bit of research i discovered the ‘getChildAt’ function, so i followed the advice and set the numbers to the index i presumed they would be in.

I now get the following error :

```auto
[SWF] Game.swf - 779350 bytes after decompression
RangeError: Error #2006: The supplied index is out of bounds.
    at flash.display::DisplayObjectContainer/addChildAt()
    at Game/addCoins()[C:\Users\Chris\Documents\Uni Year Three\Flash Games\Car Assignment Final\Final 12th Feb\Game.as:56]
    at Game()[C:\Users\Chris\Documents\Uni Year Three\Flash Games\Car Assignment Final\Final 12th Feb\Game.as:41]
TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at Car/loop()[C:\Users\Chris\Documents\Uni Year Three\Flash Games\Car Assignment Final\Final 12th Feb\Car.as:52]

```

Please help! 🙂 thanks.

```auto
        public function Game()
        {
            // CONSTRUCTOR CODE
            addEventListener(Event.ENTER_FRAME, everyFrame);// Calls this function every frame
            coinsCollected = 0; // Set coinsCollected to 0
            lifes = 3; // Set lives to 3
            mapContainer = new MovieClip(); // Create new mapContainer MC
            addCoins(); // Calls the addCoins function once 
            addMap();
        }

        // Function to add the coins to the stage, only called once
        public function addCoins()
        {
            var areas:Array = [map.area1_mc,map.area2_mc,map.area3_mc,map.area4_mc,map.area5_mc,map.area6_mc];
            for (var i:int = 0; i < areas.length; i++)
            {
                for (var j:int = 0; j < 3; j++)
                {
                    var Ycoin:Coin = new Coin();
                    Ycoin.x = Math.round(Math.random() * ((areas*.x + areas*.width - Ycoin.width) - areas*.x)) + areas*.x;
                    Ycoin.y = Math.round(Math.random() * ((areas*.y + areas*.height - Ycoin.height) - areas*.y)) + areas*.y;
                    mapContainer.addChildAt(Ycoin,-1);
                    coinsOnstage.push(Ycoin);
                }
            }
        }
        
        public function addMap()
        {
            mapContainer.addChildAt(map,0); // Adds the map to the Container.
            stage.addChild( mapContainer ); // Adds the container to the stage.
            mapContainer.x = 654.90; // Set the map Containers .X
            mapContainer.y = 169.05; // Set the map Containers .y
            stage.addChild(car); // Add the Car.
            car.x = 76; // Set the cars.X
            car.y = 530; // Set the cars.y
        }

```
