Search code examples
flashactionscript-3adobeinstancemovieclip

2 MovieClips with the same Instance Name


I have 2 copys of the same Movieclip on the timeline and I need them both to do the exact same thing, so I figured I'd give them the same instance name.

I have an event listener on the stage that listens for a mouse click and then checks what has been clicked using a switch statment, but the switch statment only picks up one instance of the movieclip, the other one comes up as the default.

Mainly what i'm asking is, is it possible to have to movieclips on the timeline with the same instance name?

public function Main() {
    stage.addEventListener(MouseEvent.CLICK, doStuff);
}

public function doStuff(e:MouseEvent):void {
    switch (e.target) {
        case myMC1 :
            //do stuff
            break;
        case myMC2 :
            //do stuff
            break;
        case myMC3 :
            //do stuff
            break;
        default :
            //do stuff
    }
}

Solution

  • Give the instances two different names (NEVER USE THE SAME NAME FOR TWO OBJECT, REALLY :)) and change the switch statement this way:

     public function doStuff(e:MouseEvent):void {
    switch (e.target) {
        case myMC1 :
        case myMC2 :
            //do stuff
            break;
        case myMC3 :
            //do stuff
            break;
        default :
            //do stuff
    }
    }
    

    By formatting it this way, you can execute the same code for two different cases