Perhaps you, like me, have had a need, at some point or another, to parse RSS feeds in Actionscript. If so, then perhaps you’ve also stumbled across the generally wonderful as3syndicationlib developed by Mike Chambers and Christian Cantrell. For those of you who aren’t in the know, allow me to provide the following excerpt from the project’s google code page:

Use the syndication library to parse Atom and all versions of RSS easily. This library hides the differences between the formats so you can parse any type of feed without having to know what kind of feed it is.

These claims actually hold up pretty well…once you get the code running.

The problem is that the codebase was written specifically for development in Flex. While Flex is a great platform for certain projects, many of us still use Flash for a lot of the work we do, so a toolkit that relies on packages that are only available to Flex just doesn’t do it.

This problem is also no secret. It’s listed as an accepted issue on its project page. The problem is that the codebase apparently hasn’t been updated since December of 2006.  That’s no joke kids.

Fortunately, a solution was pushed forth from outside the project by a Mr. Martin Legris in the form of a bare implementation of the missing “DateBase” class that the syndication library looks for, as well as an edit to an import statement buried down in ParsingTools.as.

So if you, like me, have a need to consume RSS/Atom Feeds in Flash and need a solution that works, look no further. I present, to you, an updated version of the as3 syndication library with the necessary changes in place to begin using it out of the box in your flash projects.

The files are all available here.

Categories: ArticleBugsDownloadsFlashLibrariesas3syndicationlib
Published: 07.02.10 :: No Comments »


A Runtime Error

Perhaps you’re familiar with this image. How embarassing to launch an app only to find that it’s throwing up large, intrusive error windows!

What’s to be done? Well, you have two options:
  1. Write perfect code.
  2. Use Runtime Error Handling

Some day I’ll elaborate on the secrets of the first option(1, 2), but for the time being I’d like to talk about Runtime Error Correction and Handling.

Let me begin by explaining what a runtime error is. As compared to a Compile Error, which is caught on compilation, or a Logical Error, which is due to fuzzy logic on the part of the programmer, a Runtime Error is an error that occurrs in your code while it is executing.

Flash has several types of Runtime errors that are thrown at different times depending on the type of error that occurrs. As somebody who works with Flash every day, I can say with some confidence that the most important and common ones for you to worry about are TypeErrors, ReferenceErrors, and RangeErrors. You may also find yourself occasionally encountering SecurityErrors and SyntaxErrors, but for now we’ll just worry about the first three.

How do these errors occur? Let’s break it down one by one.

TypeErrors

Type errors generally occur when you improperly coerce the value of one variable to the type of some incompatible “other” variable. What does this mean? Here’s an example:

var mov = getChildByName("myMovieClip");
mov.gotoAndPlay(2);

The code above will throw an error because you’re implicitly (rather than explicitly) trying to coerce a DisplayObject into behaving like a MovieClip. What the hell am I talking about? Well, if you check the documentation for the “getChildByName” method on the MovieClip class, you’ll see that it returns a DisplayObject. What this means is that whenever you call this method without explicitly casting its return object, Flash is going to assume you’re just talking about a vanilla, plain-jane DisplayObject.

Here are two solutions:

var mov = getChildByName("myMovieClip") as MovieClip;

…or…

var mov = MovieClip(getChildByName("myMovieClip"));

Both lines of code do the same thing. For the sake of readability, I prefer the first method.

ReferenceErrors

Reference Errors occur when you try to access or set a property that doesn’t exist on a non-dynamic class. For people who are new to object oriented programming in AS3, this will happen for a while until you really appreciate the difference between a dynamic and a non-dynamic class. Let’s look at an example.

package {
     public class Foo() {
          public function Foo() {
               trace(this.bar);
          }
     }
}

The example above will throw a Reference Error because the class Foo doesn’t have a property named “bar.” I have two solutions.


package {
public class Foo() {
     public var bar:*;
          public function Foo() {
               trace(this.bar);
          }
     }
}

In the above example, we just made sure that the public class Foo does have a property named “bar.”

package {
     public dynamic class Foo() {
          public function Foo() {
               trace(this.bar);
          }
     }
}

In this second example, we made the public class Foo into a dynamic class. What this means is that we can refer to any property we want on Foo, and if it doesn’t exist Flash will create it for us automatically. This may sound like a very tempting proposition, but the first approach is most definitely preferred on this one, which is a point I will explain at a later date. For the time being, just take my word for it: Avoid making your classes dynamic if you can.

RangeErrors

A Range Error occurs when you attempt to refer to an index that doesn’t exist in an Array. For example:

var shitty_bands:Array = [ "Slipknot", "Linkin Park", "Insane Clown Posse"];
trace(shitty_bands[3]);

The code above will generate a range error because the shitty_bands array doesn’t have any entries at an index of 3. You’ve literally referred to an entry that is out of the range of the array, making this a pretty easy error to remember. To solve, you could…

var shitty_bands:Array = [ "Slipknot", "Linkin Park", "Insane Clown  Posse"];
trace(shitty_bands[shitty_bands.length-1]);

…use properties of the array to make sure that you don’t refer to entries outside of its range, which also saves you the trouble of actually needing to know the number of entries in the array at any given point (assuming you were just trying to grab the last item in the array), or you could…

var shitty_bands:Array = [ "Slipknot", "Linkin Park", "Insane Clown  Posse", "The Black Eyed Peas"];
trace(shitty_bands[3]);

…just add another shitty band to the array.

Admittedly, these are stop-gap solutions that all depend on what you’re actually trying to accomplish with your program. There is one final solution.

Explicit Error Handling

What do I mean by Explicit Error Handling? I mean that you anticipate when portions of your code may throw errors, and set up appropriate control structures to deal with them when they happen. There are two ways to do this, and the method you choose may vary depending on the type of error you anticipate and the amount of code you’re trying to control for. Let’s consider the first method.

Error Event Listeners

Many errors can be taken care of with simple event handlers. Security Errors and IOErrors, for example, have their own event classes that you can listen for.


package {

     import flash.net.*;

     import flash.events.*;

     public class Foo() {

          public function Foo() {

               var loader:URLLoader = new URLLoader();

               loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, _securityErrorHandle, false, 0, true);

               loader.addEventListener(IOErrorEvent.IO_ERROR, _ioErrorHandle, false, 0, true);

               loader.load(new URLRequest("data.xml"));

          }

     }

     private function _securityErrorHandle(e:SecurityErrorEvent):void {

          trace("SecurityError :: ",e.errorID);

     }

     private function _ioErrorHandle(e:IOErrorEvent):void {

          trace("IOError ::",e.errorID);

     }

}

Depending on how large or complex your application is this may be a good solution for you. I know that in some respects I find this easiest to understand as it runs on the existing Event metaphor that I’m accustomed to using for everything else in AS3.

The second method employs a construct which with, if you have done any programming in the past, you are probably familiar.

package {
     public class Foo() {
          public function Foo() {
               try {
                    trace(this.bar);
               } catch (e:ReferenceError {
                    trace("Reference Error!");
               } finally {
                    // do something else
               }
          }
     }
}

The Try…Catch…Finally construct exists in pretty much every programming language known to man. It’s also pretty simple to understand. The application “attempts” to do whatever is within the “try” statement. If an error occurs, it’s handled in the “catch” statement, and whatever was going to take place in the “try” statement just doesn’t happen. After these steps, the application does whatever is indicated in the “finally” statement. It doesn’t get much easier than that. This is also nice because it forces you to deal with errors at the site of their creation, and to be more thoughtful about where and why errors happen.

Categories: Actionscript 3ArticleBest PracticesFlashFlexTipsTutorial
Published: 03.31.10 :: No Comments »


I spent the last week or so developing a kiosk application for a client that had to prevent users from exiting the application using ctrl+alt+delete, alt+f4, or any other such method.

Because I had worked with it in the past for a similar project, I advised the client that we could use Northcode’s SWFStudio to lock the application down and to record registration data without requiring an internet connection. This should have all been very simple. Create the application, load up SWFStudio, and convert my final SWF to an EXE with all the parameters configured.

In practice things weren’t so simple. You wouldn’t know this unless you dug around, but SWFStudio 3.7 doesn’t play nicely with Windows 7, and for the $300 licensing fee you’d hope they would take the trouble to make this point more prominent. To give you an idea of this problem, I’d like to demonstrate the sequence of steps you need to complete to figure out why your perfectly functional kiosk craps out after you push it through SWFStudio.

Step 1

First, you have to go to Northcode’s website. When you get there, click on the “swf studio” link at the top of the page. Don’t click “blog” and whatever you do, don’t click “support.”

Step 2

Once you get to the “swf studio” page, click to continue reading the article on making your applications work with Windows 7. Don’t bother with the “Known Problems” link in the side bar, as even though they thought to post an article about the issue (separate from their blog and their support page no less), it’s not a problem they’re aware of.

Step 3

You’re almost done! You’ve found the document explaining how to fix your problems in Windows 7. Of course, there’s no patch or software update. You have to copy the grayed out text above into a panel in SWF Studio to get everything functioning properly.

What makes me even angrier about this is the text of the article. The summary, shown in step 2, contains the following text:

Windows 7 adds support for a new section in your application manifests called Compatibility and if you want your SWF Studio applications to behave properly on Windows 7 then you’d better pay attention!

They even use an exclamation mark! I’d better pay attention if I expect their $300 application to function correctly. That might even be ok if they displayed the information prominently on their homepage, or in any of the sections you’d expect it to be in. To give you an idea of how poorly placed this content is, as I was sitting down to write this post it took me 15 minutes digging around their site to find the document in question again.

For my own satisfaction, and for the sake of anybody else who runs into problems with this software, I’ve taken the following liberties:

  1. I’m offering this code to you below so there’s a second place to find it if anybody ever needs it.
  2. I’m offering this code for download so you can keep your own copy if need be.
  3. I’m recommending a slight redesign to Northcode’s home page.

The code looks like this.


<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<assembly  xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">

 <assemblyIdentity  version="1.0.0.0" processorArchitecture="X86" name="stub.exe"  type="win32" />

 <description>stub</description>

 <dependency>
 <dependentAssembly>
 <assemblyIdentity
 type="win32"
 name="Microsoft.Windows.Common-Controls"
 version="6.0.0.0"
 processorArchitecture="X86"
 publicKeyToken="6595b64144ccf1df"
 language="*" />
 </dependentAssembly>
 </dependency>

 <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
 <security>
 <requestedPrivileges>
 <requestedExecutionLevel  level="asInvoker" uiAccess="false"/>
 </requestedPrivileges>
 </security>
 </trustInfo>

 <compatibility  xmlns="urn:schemas-microsoft-com:compatibility.v1">
 <application>
 <!-- the ID below indicates the application supports Windows 7  -->
 <supportedOS  Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}"/>
 </application>
 </compatibility>

</assembly>

You can download a file with this information here: SWF Studio Manifest for Windows 7 Projects

And my suggestion for a redesign of Northcode’s homepage is below.

The Corrected Northcode.com!

Categories: ArticleBugsDownloadsSWF Studio
Tags:
Published: 03.27.10 :: 3 Comments »