Better than nothing

Tag: Debugging Page 3 of 4

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.

Flash AS3: The simple way to create an API reference with ASDoc

I have known for some time that I could generate an Actionscript API Reference for my own class packages using ASDoc, but it’s a command-line tool and I haven’t done any command-line programming in about 12 years, so I didn’t want to deal with the hassle. Then, I found this blog post by Derek Griggs (link is gone now, but go read his blog anyway – it’s good) where he created a batch file that he could simply run when he wanted to generate the docs. Genius! And since ASDoc comes packaged with the FREE Flex SDK, anyone can do this (well, almost anyone). The following instructions are for Windows users. Sorry Apple heads.

Step 1: Download the Free Flex SDK from Adobe here

Step 2: Unzip the downloaded file and place it somewhere logical on your hard drive. I placed it in the C:/Program Files/Adobe/ folder.

Step 2.5 (Optional): open the flex config XML file found here: [flex sdk folder]/frameworks/flex-config.xml and tweak your settings. For example, you may want to set the <strict> option to false.

Step 3: Open a simple text editor like Notepad and create the following file (or download the sample packet here and use the included batch file sample):

cls
set path=C:\Program Files\Adobe\flex_sdk_4.1\bin
asdoc.exe -source-path classes -doc-sources classes -window-title "Wasted Potential Sample AS Documentation" -main-title "Sample Documentation" -footer "This is a test<br/>www.wastedpotential.com" -output docs
pause

Step 4: Save this file as “asdoc.bat” in a folder one level up from your class package(s). For example, In my Flash projects, I usually have a folder named “classes” that contains all of my class packages, so I save this batch file outside of the “classes” folder. When I run the batch file, it will create a folder named “docs.”

Step 5: Modify the file as needed. The file is simple, but I’ll explain it piece by-piece:

cls

“Clear screen” removes any other output from the terminal window.

set path=C:\Program Files\Adobe\flex_sdk_4.1\bin

This sets the path where your computer can find the executable file you wish to run. In this case, it is the location of asdoc.exe. If you placed the Flex SDK somewhere else, you will need to set this path for your configuration.

asdoc.exe

Runs the ASDoc executable

-source-path classes
-doc-sources classes

Options that tell ASDoc where to find the classes it is documenting.

-window-title "Wasted Potential Sample AS Documentation"
-main-title "Sample Documentation"
-footer "This is a test<br/>www.wastedpotential.com"

These are more options. They simply set some text in the output files.

-output docs

This option specifies the folder where your new API will be created. If it doesn’t exist yet, ASDoc will create it. There are more options than this, but these are the important ones. Click here to check out some of the other options.

pause

This simply keeps the terminal window open. Without this line, the terminal window closes and you can’t view any error messages.

Step 6: Save the file again and double-click it to run it.

Step 7: Wait a few seconds until it is done running. If your classes have errors, the script will fail. Fix the errors and try again. If it succeeds, you will now have a full set of Actionscript API docs for your classes. Open them up and check them out.

Now, you can go add valid Javadoc-style comments to your classes and you will have a professional API reference for your project. Sweet! Don’t know how to add Javadoc comments? Well, I created a sample class package and batch file for you to play with. You can download the sample here. There are also lots of resources on the web for how to write Javadoc comments. The best reference I have found is here.

IMPORTANT: ASDoc is a bit touchy and fails A LOT when trying to generate an API. Here are a few caveats:

  • Your classes must not have any compiler errors or ASDoc will fail.
  • Your javadoc comment syntax must be correct or ASDoc will fail with a cryptic error about
    “toplevel.xml”
  • You must declare stage instances explicitly. If you use Flash and have “Automatically declare stage instances” turned on in the Actionscript 3.0 publish settings, ASDoc will fail, throwing an error for each stage instance name in your classes that doesn’t appear in the variable declarations.
  • Even more annoying, if you use any open-source packages like Papervision, their syntax must also be flawless, or ASDoc will fail (in fact, ASDoc failed for me when trying to run it on a project with the Papervision3D package). For large projects, this can be a huge pain. So, you may want to run ASDoc on any open-source libraries first to make sure they compile correctly by themselves.
  • if you use any flash packages that are not in the Flex SDK, you must add them to the compiler path. For example, the Flex SDK does not include the flash “fl” package (used for motion tween, etc). You can add the paths to the batch file if you can figure out where the files are. In the case of the “fl” package, add this option to the batch file:
    -compiler.source-path "C:\Program Files\Adobe\Adobe Flash CS3\en\Configuration\ActionScript 3.0\Classes"

    (that path is “C:\Program Files\Adobe\Adobe Flash CS3\en\Configuration\ActionScript 3.0\Classes”). Then, you may need to use the -exclude-classes option to prevent the additional classes from being added to the output.

If anyone knows how to modify these instructions for a Mac, let me know and I’ll update the post.

You can download the sample files here.

Page 3 of 4

Powered by WordPress & Theme by Anders Norén