# Adding Random Library Instances to Stage Using Arrays

**URL:** <https://forum.kirupa.com/t/adding-random-library-instances-to-stage-using-arrays/332579>\
**Category:** flash\
**Created:** [August 2, 2014, 4:19am UTC](https://forum.kirupa.com/t/adding-random-library-instances-to-stage-using-arrays/332579 "2014-08-02T04:19:06Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pherank](https://avatars.discourse-cdn.com/v4/letter/p/4af34b/32.png) [@Pherank](https://forum.kirupa.com/u/Pherank)\
**Post date:** [August 2, 2014, 4:19am UTC](https://forum.kirupa.com/t/adding-random-library-instances-to-stage-using-arrays/332579/1 "2014-08-02T04:19:06Z")

</div>

It’s been a long while since I’ve used Flash or AS3 so you’ll have to bear with me. 😉

I want to create a little music sight reading helper: notes will appear randomly on the bass and treble staffs and when the note appears the correct piano sound can be heard as well (the note should appear/sound for 4 seconds and then be removed from the stage and replaced by another randomly selected note). Only one note will appear at a time in this first version of the program.

I have a Library of Movieclips that are exported to Actionscript on frame 1, and some basic code. I can successfully attach a single MC symbol to the stage using the following code:

```auto
var Middle_C: C4_Middle_C = new C4_Middle_C;
addChild(Middle_C);
Middle_C.x = 328;
Middle_C.y = 321;

```

So that much works, but I now want to create an array to hold the various instance names and add them to the stage programmatically.

This is the code I have so far:

```auto
import flash.display.MovieClip;

// Array of Actionscript instance names
var notesArray: Array = ["C4_Middle_C", "D4", "E4", "F4", "G4", "A4", "B4", "C5"];
// Array of y coordinate positions for these instances
var yPosArray: Array = [321, 303, 285, 267, 249, 231, 213, 195];

// Create timer
var timer: Timer = new Timer(4000, 3);

// Add event listeners
timer.addEventListener(TimerEvent.TIMER, addNote);
timer.addEventListener(TimerEvent.TIMER_COMPLETE, noteSoundComplete);

function getRandomElementOf(notesArray: Array): Object {
    var idx: int = Math.floor(Math.random() * notesArray.length);
    return notesArray[idx];
    trace(idx);
}

function addNote(event: TimerEvent): void {
    stage.addChild(getRandomElementOf(notesArray) as MovieClip);
}

function noteSoundComplete(event: TimerEvent): void {
    //Remove this MC from the stage
    this.parent.removeChild(this);
}
timer.start();

```

But I’m getting the following error -

```auto
TypeError: Error #2007: Parameter child must be non-null.
    at flash.display::DisplayObjectContainer/addChild()
    at flash.display::Stage/addChild()
    at PianoAudio/addNote()
    at flash.utils::Timer/_timerDispatch()
    at flash.utils::Timer/tick()

```

[http://burntscarlet.com/\_test/help\_forums/Kirupa/grand\_staff\_test.fla.zip](http://burntscarlet.com/_test/help_forums/Kirupa/grand_staff_test.fla.zip)
