user1763814
user1763814

Reputation: 11

How to access a movie clip inside another movie clip in AS3?

I'm currently making an audio player, which includes a scrolling track list.

The name of the track list is "content" and inside "content" I have multiple movieclips, named from track1-track10.

I'm currently trying to access individual movieclips inside the "content" movieclip, but I'm not getting any luck.

Sorry if this isn't very clear.

Here's some of my code:

    var trackName: String;

function playTrack(e: MouseEvent) :void{
    switch(e.target.name){
        case "track1":
        trackName = "These Days";
        trace ("track 1");
        break;

        case "track2":
        trackName = "Walking After You";
        trace ("track 2");
        break;
    }
}

content.track1.addEventListener(MouseEvent.CLICK, playTrack);
content.track2.addEventListener(MouseEvent.CLICK, playTrack);

Any help is apperciated :)

Upvotes: 1

Views: 1331

Answers (3)

user1763814
user1763814

Reputation: 11

Found that the problem was with the text I was using in the list.

After changing it from TLF text to Classic Text, it worked properly.

Upvotes: 0

HDquality
HDquality

Reputation: 26

Try e.currentTarget instead of e.target.

And by the way, since a MovieClip is dynamic, you can skip the use of a switch. Here is the code:

content.track1.trackName =  "These Days";
content.track2.trackName =  "Walking After You";
content.track1.addEventListener(MouseEvent.CLICK, playTrack);
content.track2.addEventListener(MouseEvent.CLICK, playTrack);

function playTrack(e: MouseEvent) :void{
     trackName = e.currentTarget.trackName;
}

Upvotes: 1

Mike Bedar
Mike Bedar

Reputation: 910

Keep in mind that DisplayObject.name refers to the instance name, so if you are working in the Flash IDE, make sure that the instance name in the Properties panel is filled out.

Also, try storing the name in a string var before the switch:

var obj_name:String = e.target.name;
switch(obj_name){
}

Since e.target is of type Object, the name propert will not be typed, and Switch in particular seems to hate that.

Upvotes: 0

Related Questions