Tutorial details: Using MovieClipLoader with loops and listeners
Difficulty Level: Intermediate
Requirements: Flash MX 2004
Assumed Knowledge: MovieClipLoader API Part 1
File(s) to Download: MCL2_tutorial.zip
Online Example: MCL2
MovieClipLoader with loops and listeners
Welcome to the second part of the MovieClipLoader tutorial. It's recommended (but not essential) that you work through the first part before reading any further.
This tutorial is going to start by identifying a common problem, and then we are going to use the MovieClipLoader class to provide the solution
to it. The problem is one that has been the cause of a great many forum threads since the release of Flash MX, and goes something like this:
Hi, I dynamically created a bunch of movieclips and assigned
each of them an event handler. Then I loaded content in to
these clips, but I can't get the event handlers to work!
Here's my code [insert reams of spaghetti-like code here].
What am I doing wrong Please help!!!
Okay, so first we need to identify the cause of this problem and it is this: when content is loaded into a movie clip the movie clip gets reset - this means that
any code you had previously assigned to it goes the way of the Dodo. This doesn't pose too much of a problem with movie clips that have been placed manually on stage
during authoring, because any code you have assigned directly to the movie clip, by way of an onClipEvent, will survive, but it does pose a problem for dynamically
created clips, because we have to assign event code to these remotely at run-time.
How do we get around this problem? There are various ways around it, and since there are bountiful forum threads covering the topic I won't
discuss it too deeply here, but whether it's an onLoad event or an onPress event, if we're loading external content in to flash we can achieve
what we want by taking advantage of our friend, the MovieClipLoader class.
You may recall that at the beginning of the first tutorial I said this: "If you have ever tried to load an external jpeg or swf in to a flash movie, it is
likely you will have encountered a situation where you need to trigger an event based upon completion of that loading process." We have already taken a look at
the callbacks belonging to the movieclip loader, and so hopefully you will be able to see where we're going with this. In my example we're going to be creating
functionality akin to that of a thumbnail gallery. We will load a set of jpgs into dynamically created movieclips, and we want each of these movieclips to possess
their own onPress events. Will will avoid our code-wipe-out problem by assigning the events to the clips after the content has loaded in.
|