taranpyper
taranpyper

Reputation: 35

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
    }
}

Upvotes: 0

Views: 1572

Answers (2)

Sr.Richie
Sr.Richie

Reputation: 5740

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

Upvotes: 2

The_asMan
The_asMan

Reputation: 6402

Use e.currentTarget instead.
e.target will give you the object that dispatched the event which could be a child of your MovieCLip

Upvotes: 1

Related Questions