# Using and referring to dynamically created instances

**URL:** <https://forum.kirupa.com/t/using-and-referring-to-dynamically-created-instances/309856>\
**Category:** flash\
**Created:** [May 26, 2010, 8:10am UTC](https://forum.kirupa.com/t/using-and-referring-to-dynamically-created-instances/309856 "2010-05-26T08:10:34Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![ztw](https://avatars.discourse-cdn.com/v4/letter/z/8c91f0/32.png) [@ztw](https://forum.kirupa.com/u/ztw)\
**Post date:** [May 26, 2010, 8:10am UTC](https://forum.kirupa.com/t/using-and-referring-to-dynamically-created-instances/309856/1 "2010-05-26T08:10:34Z")

</div>

Hi,

I’m creating a DDR style game. The ‘moving graphics’ are being randomly and dynamically created with the default _instance_ name (instance28, instance 30…) I want to be able to refer to these different elements according to another movie clip I press (in this case, they look like piano keys).

So, if a ‘C’ moving graphic is created, I want the C movie\_clip with a listener attached to check the ‘C’ moving graphic.

stop();

//random notes  
var noteTimer = 0;  
var note;

stage.addEventListener(Event.ENTER\_FRAME, frameTrigger);

function frameTrigger (evt:Event) {

//randomly spawn notes  
noteTimer++;

```
if (noteTimer == 10) {
        //make a random number
        var ranNum:Number = Number(Math.round(Math.random() * 2));
        
        if (ranNum == 0) {
            //rest
            trace("rest");
            }
        
        else if (ranNum == 1) {
            //snowflake 1
            note = new Csign();
            trace("Csign");
        
        } else if(ranNum == 2) {
            //snowflake 2
            note = new Dsign();
            trace("Dsign");

        }
    
    note.x = 685;
    note.y = 353;
    
    addChild(note);
    noteTimer = 0;
    trace(note.name);
}

```

}

//piano note listeners  
C\_mc.addEventListener(MouseEvent.CLICK, playC);  
Csharp\_mc.addEventListener(MouseEvent.CLICK, playCsharp);  
D\_mc.addEventListener(MouseEvent.CLICK, playD);

//button modes  
C\_mc.buttonMode = true;  
Csharp\_mc.buttonMode = true;  
D\_mc.buttonMode = true;

//functions for listeners  
**function playC (evt:MouseEvent):void {  
trace(“C”);  
/_if (note.x \> 200 && note.x \< 300){  
trace(“well done”);  
} else {  
trace(“bad!!”)  
}_/  
C\_mc.gotoAndPlay(2);  
}**

function playCsharp (evt:MouseEvent):void {  
trace(“C#”);  
Csharp\_mc.gotoAndPlay(2);  
}

function playD (evt:MouseEvent):void {  
trace(“D”);  
D\_mc.gotoAndPlay(2);  
}

Any help is appreciated!
