Sunday, July 1, 2007

(flash) Using two different classes with identical names is a bad idea

I'm working on a game that has a pretty complicated Help screen, so we separated the game SWF and the help SWF. Both the game and the help should run in different languages, so I wrote a helper class that translates text fields automatically. Each SWF has its own version of the class, with the same name.

Now, I had to modify the translator class in the Help a bit to accommodate HTML parsing, and it ran beautifully standalone. But when I load the Help from the game SWF, I suddenly see that every textfield that should go through HTML parsing just doesn't show any text!

A little tracing reveals the culprit: I placed a trace on the constructor of the translator class of the Game and a different one at the translator class of the Help. When the help was loaded, the game's translator class constructor was called, instead of the Help translator class!
Changing the name of the help translator class fixed the problem, of course.

To summarize, the second class is simply overridden by the first class!
This is how I found this out:


Wow now when it's down on chart it seems so obvious! But I haven't read about this behavior anywhere yet.

This may sound like a little anecdote, but I'm thinking about the Hasbro project I was involved in, where we had a game system SWF that loads all kinds of different game SWFs onto itself that were produced by all sorts of different companies. If one of these SWFs had a class with a name the game system used, there would be DISASTER!

Like my Boss, Itay, said: Prefix = good idea.

No comments: