# Why doesn't this work?

**URL:** https://forum.kirupa.com/t/why-doesnt-this-work/321389
**Category:** Uncategorized
**Created:** [April 18, 2011, 9:38pm UTC](https://forum.kirupa.com/t/why-doesnt-this-work/321389 "2011-04-18T21:38:32Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![Blargh](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Blargh](https://forum.kirupa.com/u/Blargh)
#### Post date: [April 18, 2011, 9:38pm UTC](https://forum.kirupa.com/t/why-doesnt-this-work/321389/1 "2011-04-18T21:38:32Z")

</div>

I’m trying to spawn multiple ovals all at random points. It displays only one oval and gives me

```auto
RangeError: Error #2006: The supplied index is out of bounds.
    at flash.display::DisplayObjectContainer/getChildAt()
    at boxxy_fla::MainTimeline/posOvals()
    at boxxy_fla::MainTimeline/addOvals()
    at boxxy_fla::MainTimeline/frame1()

```

```auto
import flash.events.KeyboardEvent;

stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);

var oval: ovally = new ovally();
var container = new Sprite();

addOvals();

function addOvals()
{
    addChild(container);
    var numOvals = Math.floor(Math.random() * 10);
    for (var i = 0; i < numOvals; i++)
    {
        container.addChildAt(oval , i);
        posOvals(i);
    }
}

function posOvals(indexOfOval)
{
    var xPos = Math.floor(Math.random() * 550);
    var yPos = Math.floor(Math.random() * 400);

    container.getChildAt(indexOfOval).x = xPos;
    container.getChildAt(indexOfOval).y = yPos;
}

function keyPressed(evt:KeyboardEvent):void
{
    if (evt.keyCode == Keyboard.RIGHT)
    {
        boxxy.x += 5;
    }
    else if (evt.keyCode == Keyboard.LEFT)
    {
        boxxy.x -= 5;
    }
    else if (evt.keyCode == Keyboard.UP)
    {
        boxxy.y -= 5;
    }
    else if (evt.keyCode == Keyboard.DOWN)
    {
        boxxy.y += 5;
    }
    if (boxxy.hitTestObject(oval))
    {
        boxxy.gotoAndPlay(2);
        container.removeChild(oval);
        stage.removeEventListener(KeyboardEvent.KEY_DOWN , keyPressed);

    }
}

```
