Fixing the NetStatusEvent.info.code 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 ‘NetstatusEvent.info.code‘ 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 (e.info.code){ case"Netconnection.Connect.Success": trace("You connected!"); break; case "Netconnection.Connect.Closed": trace("You disconnected!"); break; } }
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 ‘event.code.info’ 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 (e.info.code){ case NetStatusInfo.NETCONNECTION_CONNECT_SUCCESS.code: trace("You connected!"); // returns the code property (same as 'toString()') trace(NetStatusInfo.NETCONNECTION_CONNECT_SUCCESS.code); // returns the level property trace(NetStatusInfo.NETCONNECTION_CONNECT_SUCCESS.level); // returns the meaning of this status change trace(NetStatusInfo.NETCONNECTION_CONNECT_SUCCESS.meaning); break; case NetStatusInfo.NETCONNECTION_CONNECT_CLOSED.code: trace("You disconnected!"); break; } }
However….
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 event.info.code 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:
- No more copy-pasting of ‘codes‘. They’re just constants in the NetStatusEvent class.
- 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!
- 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…
Download
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!
Enjoy!
Download: Click
Related Posts
5 Comments
Trackbacks and Pingbacks
-
[...] NetStatusInfo class [...]























Nice post. I agree with all your points.
thats really very helpful…………………..thankssssss
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
Hey, this piece of code really helps me a lot.
Many thanks for the distribution to us!