Skip to main content
Known Participant
April 10, 2007
Question

Problems attaching event handler to MovieClip

  • April 10, 2007
  • 3 replies
  • 358 views
I have a bit of thumbnail generation code that looks like this:

for (var i = 1; i <= 5; i++) {
filename = "Images/Thumbnails/thumb" + i + ".jpg";
curr_thumb = thumbstrip_mc.thumbnails_mc.attachMovie("thumbnail", "thumb" + i);
curr_thumb.loadMovie(filename);
curr_thumb.onRelease = sayHello;
curr_thumb._x = ((i - 1) * 125) + 10;
curr_thumb._y = 25;

}

Basically, the structure of my code is as follows:

_level0.thumbstrip_mc = elements of a thumbnail strip. This includes a background movie clip, a bar with an arrow pointing either up or down depending on whether the strip is open or closed, and a collection of thumbnail images.

_level0.thumbstrip_mc.thumbnails_mc = a container for all the thumbnails

_level0.thumbstrip_mc.thumbnails_mc.thumb1 (thumb2, thumb3 ... thumb 5) = the thumbnail images themselves.

I want to attach an onRelease event handler onto thumb1, thumb2, thumb3, etc... At the moment, the event handler directs to a simple trace function called sayHello(). However, it doesn't work. My mouse does not change from an arrow to a hand, indicating thumb1.onRelease is not handling any events. I even commented out all other event handlers on that level to make sure it wasn't getting intercepted by another event handler on a parent timeline, but that wasn't the problem.

I'm guessing, then, that my problem must have something to do with the loadMovie method. It's wiping out my handler, right? I'm guessing the handler is attaching before the movie clip is dynamically loaded in? What's the easiest way to fix this? Or is it another problem?
This topic has been closed for replies.

3 replies

Known Participant
April 10, 2007
Gotcha. That's kinda what I was figuring, as I ran into a similar problem with trying to alpha fade dynamic contact and got around the problem by creating a placeholder which I faded instead.

So then, my two choices would be to create a placeholder for each thumbnail instance or to use something like the MovieClipLoader class I assume. I haven't played with MovieClipLoader yet, but I can go out and copy some code and figure it out. Are there any advantages/disadvantages to using one method over another?

As for classes, I'm not in any sort of class--just teaching myself ActionScript.

Inspiring
April 10, 2007
That would be because you are using loadMovie(). Loading content into a clip will remove any event handlers you have put on a clip. So you either need to wait until the external content has finished loading and then assign the onRelease handler. Or you need to assign it to a different clip and load the external art into a holder clip or some such inside of the clip that you assign the handler to.

PS: Are you taking the same class as Junio Vitorino?
Participating Frequently
April 10, 2007
Where is the sayHello function?
Known Participant
April 10, 2007
It's just a trace. I didn't think it was necessary to include. If you'd like you could put in

curr_thumb.onRelease = function () { trace ("Hello!"); }

if you'd like.