Quick Tip: Compile a Debug Version of Your Flash Project

Quick Tip: Compile a Debug Version of Your Flash Project

Tutorial Details
  • Difficulty: Intermediate
  • Program: Flash CS4
  • Estimated Completion Time: 15 mins

In this Quick Tip I’ll show you how to use Config Constants to compile different debug and release versions of your code, so you can easily switch between them.


Step 1: Config Constants Settings

A lot of people don’t know about Flash’s Config Constants because they’re new to CS4 and because they’re buried deep within two sets of tabs and two different windows. Here’s how to find them:

In your Flash file, open the Publish Settings (File > Publish Settings…) and click the Flash tab. Click the Settings button next to the Script: Actionscript 3.0 pulldown.

In this new window click the Config constants tab at the far right.

Where are the Config Constants?

Whew…


Step 2: Add the DEBUG Constant

You should see one constant (FLASH_AUTHORING) already in the list. Now we’ll add one of our own.

Click the plus button to add a new constant and give it the name ‘CONFIG::DEBUG’. In the Value field enter ‘true’.

Adding the DEBUG Constant

The interface here is pretty poor. If you’re having trouble getting the Value field to gain text focus try double clicking somewhere in that giant empty space to the right of the constant you just created. A little higher… more to the right… there! Remember CS5 is out soon – maybe they’ll have fixed this.


Step 3: Use the Constant in Your Code

Now that we have the DEBUG constant set up in our Flash file we can use it anywhere in our code:

CONFIG::DEBUG {
	trace("This code will only be compiled if DEBUG is true.");
}

The code that you place in between the braces will only be compiled into your SWF if the DEBUG constant is set to true.


Step 4: The Debug Publishing Profile

You can go into the Publish Settings and change the value of the DEBUG constant each time you want to switch between compiling a release and a debug version, but that quickly becomes tedious. To save some time you can set up a couple of different publishing profiles.

Go back to the Publish Settings and Click the ‘Rename Profile’ button. For some reason the icon for this is an ‘i’.

Name your profile ‘Debug’.

Rename Debug Profile

While you’re in here you might want to set some of the other options that will be handy for a debug build, like Permit debugging. I also find it helpful to change the audio compression settings for faster publishing of debug builds.

Permit Debugging

Step 5: The Release Publishing Profile

Once you have your Debug profile set up, hit the ‘Duplicate Profile’ button and name the new profile ‘Release’.

Release Profile

Define the settings for your release version. (Turning off Permit debugging, etc.)


Step 6: Change the DEBUG Constant

Go back to the Config constants in your Release profile and change the DEBUG constant’s value to ‘false’.

Change the DEBUG value

Conclusion

That’s it! You now have an easy way to switch between debug and release versions of your code.

This can be really handy in situations where you need different code in your program while you’re testing it than you’ll have in the final version. For example, if you’re game gets some of its data from a server or from another SWF that you don’t have access to while testing, you can feed in fake data to the Debug version.

You could also use it to set your game to start on the level you’re trying to test, without having to play through the entire the game.

When you’re done testing, just switch back to the Release profile and republish.

Tags: Tips
Add Comment

Discussion 12 Comments

  1. jimb0 says:

    Cool tip, thanks!!

  2. André says:

    Heyy, great tip dude!!!

    I will give another tip here:
    You can test if the flash is under the flash authoring, stand alone or in a browser, you can do it by testing the Capabilities.playerType, try:

    some_text_box.text=Capabilities.playerType;

    if it returns “External” it is running on flash IDE (Flash authoring), if it returns “StandAlone” it´s running on flash plugin installed in the machine and if it returns “ActiveX” it´s running on a flash plugin on IE browser, if it returns “Plugin” it´s running on another browser (Safari, crhome, FF,…)

    • Alex says:

      Andre, this is a much safer way to add debug code in. I’ve worked with developers who do debugging by adding config constants and 9 times out of 10, the project gets deployed with debug code still active.

      • Cadin says:
        Author

        I disagree.
        André’s method is fine, but it actually *guarantees* that the debug code will be deployed in the final version. 10 times out of 10. It might not be running, but it’s still there. Depending on what your debug code actually does this may or may not be an issue.

        Config Constants are resolved at compile time, which means if the constant is false, the debug code won’t be included in the SWF at all. There’s no way it can be run, and a person can’t decompile the swf and see whatever secrets are held in your debug settings.

        Mistakenly deploying the debug build is a human error, not a fault with using Config Constants.
        To avoid this I often change the swf name in the Debug publish profile. So if I accidentally forget to switch back to the Release build the published swf will be called ‘MyGame_debug.swf’ instead of ‘MyGame.swf’. That makes it pretty obvious I’ve made a mistake when I go to upload the swf.

        All that being said, you should use whatever method works best for you.

      • André says:

        The Cadin´s method is a debug method, it checks if the constant is true, if it´s true it will aggregate the code wich is between the brackets, of if false it will ignore and the code wont be in the code in final .swf, it´s a great method when you want to create a project wich will be the same project for air and website for example… you can use for example
        CONFIG::AIR{ //code here; }
        and
        CONFIG::WEB{ //code here; }
        And just create two profiles, the profile AIR wich set the CONFIG::AIR to true and CONFIG::WEB to false and the profile WEB wich you can set the CONFIG::AIR to false and CONFIG::WEB to true…

        The tip what i gave, the code will be aggregated in the final .swf file always, it´s for example to check if the .swf file is being running on flash IDE, or browser for example… if you want to use the same final file for more tham one porpouse, or for forgotten persons…
        For example: i want to load/send something from a php file, so in the Flash IDE i have to use the whole address (http://localhost/contact.php) to test, and on browser i need to use just the target file (contact.php), so i have a file called host.as wich is inside my classpath, i use like this:
        var url:String=host(“http://localhost/||contact.php”);
        It will check if is being used in Flash IDE or on a browser, if Flash IDE or StandAlone, this function returns “http://localhost/contact.php”, but if is running on browser it will return “contact.php”… note the || between the address, it will be splited in the host.as file

  3. kaniadona says:

    how to build video conference using adobe flex, like http://www.omegle.com.

    thanks……

  4. WebGuide4U says:

    thanks for this tutorial as i’m in search of debugging a flash file and i got it from here

  5. André says:

    I was almost forgetting…

    The CONFIG is a namespace automatically added by Flash, you can also create your own compiler config by using:

    config namespace OWNCONFIG;
    them:
    OWNCONFIG const TEST=true;

    Them you can use like the config…
    OWNCONFIG::TEST{
    //Code here….
    }

    Also you can create constants directly on CONFIG namespaces directly in your .as or .fla file, for ex:
    CONFIG const TEST=true;
    CONFIG::TEST{
    //Code here…
    }

  6. André says:

    I think, if combine this tutorial, with the new tutorial of extending flash with JSFL that Michael did and posted today, maybe we can create a custom debugger, just click on commands and click something like “test debugged”, so it generates a custom file debugged… i will try to find more if the jsfl can change the constant from the config´s namespace…

Add a Comment

To add a code snippet to your comment, please wrap your code like so: <pre name="code" class="html">YOUR CODE</pre>. You can replace the class name with "js," "css," "sql," or "php." If there are any "<" or ">" within your code, please search and replace them with: &lt; and &gt; respectively.