Linking Instance Names with Linkage IDs
I’ve always been an advocate of naming Flash elements meaningfully — everything from timeline layers to Library assets (including Library folder names!). If a FLA has any complexity at all, it’s simply cruel not to. Yes, cruel. If you’ve worked on complex FLAs without named assets, you’ll know that’s not too harsh an assessment.
Sure, it takes a bit more time to tack on expressive labels everywhere, but after a while, the effort becomes second nature, and the payoff is significant. Recently, this habit led to an unexpected benefit for me.
I was working on a presentational tool for a client who wanted ActionScript kept to a minimum. This FLA, when finished, was supposed to be “easily editable by a designer,” and “not ‘scary’ to someone who doesn’t know how to program.” To make things interesting, this tool was actually fairly complex, involving numerous — we’re talking hundreds — of company-specific terms whose definitions were to appear as tool tips when a term was rolled over. Normally, I would have put all these terms and their definitions into an XML file or database and created their text fields at runtime. Actually, in order to allow for rollover behavior, each text field would have to be wrapped in a movie clip, as the TextField class doesn’t feature a rollover event; in any case, all perfectly do-able with ActionScript. For this project, though, each term (meaning, each text field wrapped in a movie clip) and each tool tip (same set up) needed to be present in the Library. As you can imagine, I named these assets dutifully.
Library symbol names for the terms (usually a short phrase) were labeled after the pattern txt Lorem ipsum dolor sit amet — an arbitrary prefix txt followed by the first few words of the phrase. Tool tip symbols were named the same, except the prefix txt became tip. This made it easy to see term / tool tip pairings in the Library, as they’d show up one after the other.
I did allow myself the luxury of attaching tool tips to the Stage at runtime. The designer wouldn’t have to know how they got to the Stage, only that txt Etc symbols were paired with tip Etc symbols. If a typo was discovered, or if text color, font, or content was supposed to changed, the designer would simply have to locate the relevant asset(s) in the Library for editing.
Now, to be able to assign functions to each term’s MovieClip.onRollOver event, each term would naturally require an instance name. I decided on a very similar pattern for instance names, basically just camel case: txtLoremIpsumDolorSitAmet. This is where it got a little interesting. I certainly didn’t want to burden the designer with having to wire up event handlers …
txtTermA.onRollOver = function() {
// code here
}
txtTermB.onRollOver = function() {
// code here
}
txtTermC.onRollOver = function() {
// code here
}
… so I made sure to nest each group of terms inside a container movie clip. In this way, I could target these parent clips with my own ActionScript and assign event handlers dynamically, as discussed in “How to Reference Objects Dynamically.”
for (var clip:String in parentClip) {
var mc:MovieClip = parentClip[clip];
if (typeof(mc) == "movieclip") {
if ((mc._name).indexOf("txt") > -1) {
// code here
}
}
}
See what’s going on, here? The for..in loop cycles among all objects inside parentClip, which is the instance name of a movie clip that contains potentially dozens of text field movie clips. An arbitrarily named variable, mc, is set to a particular object in parentClip by way of the array access operator ([]). This dynamic reference, mc, is checked to see if it’s indeed a movie clip, as the parent clip might also contain other objects. Finally, mc’s MovieClip._name property is run against String.indexOf() to see if its instance name contains the letters txt. If so, bingo: we know this is one of the movie clip–wrapped terms that needs a tool tip.
Now. In this dynamic roll-up, how could I possibly associate each term with its correlating tool tip? Each tool tip (that is, each tool tip text field wrapped in a movie clip) needs to have a Linkage identifier. It’s the Linkage identifier that allows it to be pulled from the Library at runtime.
It occurred to me that careful, consistent asset naming could be put to very good use, here. I based each tool tip’s Linkage ID on the same camel case format I used for the terms’ instance names; that is, tool tip Linkage IDs were tipLoremIpsumDolorSitAmet. This meant I could use that mc._name property for two reasons: first, to check if the given object was a term; second, to pull the necessary tool tip from the Library.
String.substring() returns an abbreviated string from a given string. By using this …
mc._name.substring(3)
… I was able to pull all characters after the first three of each term clip’s instance name. I could then simply precede those characters with the string “tip” to locate the right tool tip:
// bunches of tool tip code, not shown
mcToolTip.attachMovie("tip" + mc._name.substring(3), "tip", 1);
December 6th, 2006 at 11:00 pm
DAVID I am trying to link a sliding vertical menu item to a root.. here is my file loop and array items… I grabbed a sample from Flashkit but I cannot figure it out by myself..
ti = 40;
// ————————————————
// set up some demo items
n = 0;
while (Number(n)
December 6th, 2006 at 11:01 pm
ti = 40;
n = 0;
while (Number(n)
December 6th, 2006 at 11:32 pm
EDDY,
I’m not sure, but it looks like part of your code is missing. In any case, I don’t really see what you’re asking.
I have to say, though, if you’re working through a FlashKit sample, almost anything could be going on. FlashKit is a huge repository, and much of its content is old (some of it way old). Does your question relate to this Linkage IDs article?
December 7th, 2006 at 9:46 am
Is there any way I can send you the flash file so you can look at it or would you mind looking or getting the sample from the link
http://www.flashkit.com/movies/Interfaces/Menus/Vertical-Degrees-251/index.php
Thanks a lot for your response
Eddy
December 7th, 2006 at 9:52 am
This is my best shot at the question. I am trying to link a variable to a root container in my main Scene, something that makes it load in that container when I press the button in the sliding menu…
Where, after the loop, I set the values for Item1=”blah”, Item2=”blah” and links these ones to the root container..
Uff…This question is even hard to explain…
Thanks again
December 7th, 2006 at 2:00 pm
Eddy,
I’m happy to answer questions publicly on the Adobe forums and on this blog, because onlookers can benefit from the discussion as much as the person who asks the question. In this case, however, it looks as if you’re asking a question that isn’t related to the article at hand. For example, I don’t see any mention of Linkage IDs.
In addition — and I can empathize with this — you say you’re having a hard time explaining what you’re after. It turns out that’s a fairly hefty road block! No matter where you go for help, you’ll find the greatest success when able to dissect your problem into small, manageable pieces. This is especially true for programming, because programming is arguably summarized by a single goal: to communicate effectively with the compiler. Communicating about your intent, and communicating effectively with other developers, is just as important, because that paves the way toward actually solving your problems as you encounter them.
I feel I’m the most helpful when I assist someone hands-on. It’s different when I’m the one looking at files and figuring things out on my own. That’s when I’m a consultant — which is the way I make my living.
December 7th, 2006 at 2:07 pm
Unfortunately, the part of the code I entered in the message box , for some reason it would not display on the submmited message… As I did twice before… But still thanks a lot for your concern.
Eddy
December 7th, 2006 at 2:35 pm
Eddy,
No worries.
Keep your chin up about the ActionScript. I personally find FlashKit to be both a blessing and a curse. It’s a humongous repository of sample code (among other valuable assets), but again, much of those samples are old and un(der)documented. In many cases, FLAs aren’t especially well put together, so it’s somewhat luck of the draw if what you’re getting is even worth learning. Though it takes more effort up front, I’m a strong proponent of building things from scratch. It pays off in spades, because you know exactly what you’re getting.