Fixing the ugliness in Actionscript 3

Everything in AS3 is pretty straight forward once you get your head around it but the guys at Adobe seem to have become sloppy when they wrote the NetstatusEvent structure.
Ever since I got started with Red5, remote shared objects and all kind of different remote animals about 3 years ago, I ran into this ugly ‘‘ thing which is basically just a string. In order to know which one it is you have to use a switch statement to figure this out using real strings (instead of class constants) (which looks like the following).

private function onNetStatus(e:NetStatusEvent):void{
	switch ({
			trace("You connected!");
		case "Netconnection.Connect.Closed":
			trace("You disconnected!");

As a very lazy developer, I tend to think that comparing strings like this is just crazy. And ugly! So I made my life just a little better by creating my own class that holds ‘InfoObjects‘ with the ‘code‘, ‘level‘ and ‘meaning‘ properties. That way you can just compare the ‘’ to a class constant. No need for copy-pasting codes from the Adobe site, or debugging your code in order to find you made a typo in one of those strings…

I ran the table (listing all of the NetStatus codes) through a simple XML parser and ended up with a very neat Actionscript class (which I’m sharing, so it’s up for grabs! Download link at the end of this blogpost!). Using it looks way more natural!

private function onNetStatus(e:NetStatusEvent):void{
	switch ({
			trace("You connected!");

			// returns the code property (same as 'toString()')

			// returns the level property

			 // returns the meaning of this status change
			trace("You disconnected!");


Why isn’t this just event based? Right now the Netconnection dispatches a NetStatusEvent.NET_STATUS. Most people find this very acceptable. I, myself, tend to think it’s funny. The classname (NetStatusEvent) is pretty darn clear about the fact that the event triggered is related to the netstatus… Duh?!
Anyway: after having listened to that event and having caught it, we now need to evaluate the property (which will return a string like ‘NetConnection.Connect.Success‘)… Why is that??
Why not just dispatch NetStatusEvent.NETCONNECTION_CONNECT_SUCCESS? Wouldn’t this be more consistent with the rest of Actionscript?

It would only make life better:

  1. No more copy-pasting of ‘codes‘. They’re just constants in the NetStatusEvent class.
  2. No chance of runtime bugs because of typo’s made in the string itself. The use of constants makes sure the compiler knows when you’re doing something horrible, and allows him to throw that error right in your face!
  3. Auto-completion! Because life should be made easier by computers; not harder!

I think that an implementation like mentioned above would be pretty cool (and perhaps more logical). At least to me…


Feel free to use, modify, marry this class (and pass it on onto your children) if needed. If I overlooked anything or there’s a better way of doing this, feel free to ping me and I shall correct myself asap!


Download: Click


view all posts

Ronny is a freelance frontend developer with a wild passion for creativity and a relentless hate against flat design. Ronny spent years as a Flash developer before moving to HTML5 and rediscovering fun and happiness.

8 Comments Join the Conversation →

  1. Bozo

    Nice post. I agree with all your points.

  2. gaurav

    thats really very helpful…………………..thankssssss

  3. Thomas

    Thank you so much for this tutorial, it helped me alot to get started with red 5. Have now installed it on Windows, Linux and that horrible Apple thingie, works every time.

    Good Game

  4. zorou

    Hey, this piece of code really helps me a lot.

    Many thanks for the distribution to us!

  5. Guinetik

    i still think there should be different event types for each status dispatched. when you’re working with two net stream instances and you need to compare those info objects, you’ll know what i mean. great job, btw đŸ˜€

  6. Konstantin

    Well, it would be much better if it returned objects created just once instead of creating new objects everytime. Not very good for realtime interaction with tens or hundreds of messages per second

    • Ronny

      Hey Konstantin,

      I wrote this about 5 years ago, when I was way less experienced with large applications. Of course you are completely right. However, since I no longer do any Actionscript work, I’m afraid I won’t be able to help you out. But I’m sure you know what to do đŸ˜‰ Feel free to share your updated version here as well.

      Reply (in reply to Konstantin)

Leave a Reply

Your email address will not be published. Required fields are marked *