We'll give you something to cry about

Category: Flash Page 4 of 8

Flash AS3: Rename Class Package component (MXP)

I recently had a need for a Flash tool that would search through all of the files in a project and rename a class package reference everywhere that it showed up. The reason is simple: I have a generic Flash project that I use as a starting point for all of my Flash builds. It has a lot of basic features built in, such as error handling, preloading, analytics, basic page and navigation setup, etc.  It saves me several hours of coding on each new Flash project. The only downside is that all of the AS3 class files are in a package called “com.sampleclient.” So, I spend about 20 minutes renaming this class package reference in the FLAs and the Actionscript files every time I start a new project.

Well, I created a handy component to automate the process and I thought I’d give it away for other people to use. Simply download and install the MXP file found here. Once you’ve done that and restarted Flash, you will see a new entry in your Commands menu cleverly called “Rename Class Package.” Click it and it will open the following dialog box:
Rename Class Package
Enter the old package name (the one you want to rename) and the new package name (the new name you want it to have). Uncheck the box if you want it to prompt you each time it tries to save changes to an FLA file. Click OK and a file browser dialog will open:

rename class package browse

Simply find the folder that contains your project. You want the folder you choose to contain both the FLAs and the Actionscript class files. It then gives you a final double-check dialog:

rename class package check

I added this because I found myself clicking through without paying attention and specifying my entire C: drive as the project folder. So, this is one last verification to prevent that sort of thing.

It then recursively searches all files in that folder (and all of its subfolders). It does a find-and-replace for that package name in all actionscript files. It also moves all of the files in the old package to a new folder that reflects the new package name. Then it opens all FLAs and updates all linkages, base classes and document classes. Please note that it does NOT search timeline code for references to a class package. You shouldn’t be putting anything like that on the timeline anyway.

Once it has finished, it lets you know which files were updated (in case you’re interested):

rename class package confirm

It also ignores Subversion folders in the project folder. So, if you use Subversion for your flash projects, it should play nice. The current version of this component does not work with the XFL format, however. Sorry. I tried adding that functionality,but the script ran out of memory and crashed every time. I think it choked on all of the extra files and folders that are created by an XFL file.

This component does have one bug. After moving all of the Actionscript files to the new package folder, it should delete the old package folder, but it doesn’t. I don’t know why, but if I figure it out, I’ll update the component.

Hopefully, this will save you some time as well. If you want more details on how I built this, I will be posting info and source files soon. If you have any feedback for me, leave it in the comments. Thanks.

Flash CS5: TLF textfield bugs (or TLF WTF?)

So, I just upgraded to Flash CS5 and I have to say that it sucks slightly less than CS4. The IDE layout changes are for the better (unlike CS4) and it *seems* faster. But, I ran into a weird bug with one of the new features in Flash CS5: The new TLF textfield causes problems with externally loaded SWFs.

Here’s the scenario: I have a main SWF that loads another SWF at runtime. I’m using some code that I cut-and-pasted from another project to load the SWF, so I know it works. But, in my new CS5 project, it throws runtime errors whenever I try to access a public function of the loaded SWF. It can’t access any of the public methods and it keeps telling me they don’t exist. I try loading a different SWF and no problems. Huh?

In my debugging, I also noticed that when I traced the numChildren of my loaded SWF, it was wrong. It said I had 2 children when I could see that there were 4. I added another one to the timeline. It still said there were 2 children. Even weirder is that when I traced these 2 children, Flash told me that one of them was a Loader (it wasn’t). Again, I tried the same test on another SWF and it was ok.

The problem turned out to be 2 textfields that were in my loaded SWF. Flash CS5 set them as TLF Text when I created them. This is apparently the new default. The TLF text fields were breaking my loaded SWF. If I removed them, everything went back to normal. I changed the textfields from TLF Text to Classic Text and everything worked. I probably spent over an hour tracking this down.

Once I figured out what was causing the issue, I did some Googling and found this:
Flash CS5 TLF Engine Causes Errors With Loaded SWFs.

Anyway, this is a long winded way of saying be careful about using the TLF Text in Flash CS5. Oh, and…
Adobe, you suck.

Flash AS3: Stop using “Automatically Declare Stage Instances”

If you are compiling in the Flash IDE, you need to turn OFF the “Automatically Declare Stage Instances” option. It’s simple to do… go to File>Publish Settings, then click on the “Flash” tab and click the “Settings” button next to the Actionscript 3.0 dropdown. Simply uncheck the box and you’re done. It’s a crutch that is turned on by default in Flash and using it is a bad idea. Here are 4 quick reasons not to use it:

  1. No more mystery variables in your classes. If you ever have to hand off your work to someone else, it’s really helpful to have all stage instances declared in your classes:
    public var foo_mc:MovieClip //foo_mc is a stage instance
    

    It’s just a courtesy to other developers who might have to deal with your code.

  2. It fixes SOME inheritance problems. Flash is a little funny about class inheritance and it sometimes chokes on things that seem like they should be valid subclassing to me. But, I have found that I have a lot less trouble with it since I started explicitly declaring all stage instances.
  3. Your code will integrate better with a Flex environment. I don’t use Flex (now FlashBuilder) much, but I do sometimes build fancy schmancy animated components for use with Flex. You have to declare all stage instances in your class variable declarations for this to work. You might never have to deal with this, but why not create a workflow that is more portable?
  4. ASDocs requires it. If you want to create documentation for your classes using ASDocs, you must have all of your stage instances declared. Otherwise, it throws an error on each one that you didn’t declare.

I’m sure there are even more reasons than this, but those are the ones that I’ve found. Leaving “Automatically Declare Stage Instances” turned on is lazy, so knock it off.

Flash AS3: Track SWF memory usage and framerate with SWFProfiler

This is just a quick post to let you know about another essential AS3 class you should download right now. It’s called SWFProfiler and it adds this slick memory usage and framerate monitor to your SWF:

SWFProfiler Shane McCartney

Shane McCartney has built this clever component and released it for free. It is completely self-contained in it’s own class file. So, using it only requires 2 lines of code:

import com.lia.utils.SWFProfiler;
SWFProfiler.init(stage, this);

I should also mention that it is automatically added to the context menu of your SWF, so you just right-click to show it or hide it.

To see the SWFProfiler in action, go to Shane’s blog.

You can download the most recent version of the SWFProfiler class from google code. Shane has a few other goodies there as well.

Page 4 of 8

Powered by WordPress & Theme by Anders Norén