Archive for April, 2007
This discussion is necessary in order to continue with our network programming lessons.
Casting (converting one variable type to another) in ActionScript 3 is very easy. The following code casts a uint as a String:
- var nNum:uint = 5;
- var sNum:String = String(nNum);
But what if you don’t know what the variable should be cast as? For example, let’s say you have a client-server application. The server sends you an XML packet that contains the data for a variable and it tells you what the type of the variable should be with a written String expression. Your goal is to store this data in a variable of the proper type.
First, you have to know about the * (Special Type) operator. This can be used to create a variable that is “untyped.” For example, the following function can take any type of variable as a parameter:
- public function helloUntypedVariable(myVar:*):void
- trace("hello " + String(myVar));
If you have an untyped variable you can determine which type of variable it is at runtime using the is operator:
- public function customTypeOf(dataObject:*):String
- if (dataObject is uint)
- return "uint";
- if (dataObject is String)
- return "String";
- var n:uint = 5;
- var s:String = "Hello World";
- trace( customTypeOf(n) ); // traces "uint"
- trace( customTypeOf(s) ); // traces "String"
It’s important to note that the “is” operator returns true for anything and everything that the variable in question is qualified to be. For example, the following code all returns true:
- var n:int = 5;
- trace(n is uint); // traces "true"
- trace(n is int); // traces "true"
- trace(n is Number); // traces "true"
- trace(n is String); // traces "true"
Even though n is of type “int”, it even returns true in the statement “n is uint” because the value is positive and therefore qualifies to be a uint.
Now we know enough to create our “CustomCaster” utility class. This static class has two purposes: to determine the most restrictive type of a variable (CustomCaster.customTypeOf) and to cast a variable as some other data type that can be specified at runtime (CustomCaster.customCast):
Download the latest version here.
Today, I give you a free utility class that handles everything you need to handle in order to establish a connection. Tomorrow, I give you
the world a class that make it seem like you are calling functions directly on the server and visa versa.
Download the latest version of the ConnectionEstablisher class here.
- // To establish a new connection, do the following:
- var _server:ConnectionEstablisher = new ConnectionEstablisher("127.0.0.1", 4500);
- // To send data to the server, do the following:
- var xmlPacket:XML = <car>
- <model>Baja 1000 FJ Cruiser</model>
Although I went on a rant about Adobe yesterday, I really do like and respect the company–even more so today. If you haven’t heard yet, Adobe just open sourced the Flex SDK as of this press release. This is definitely a bold move. From the release:
“The definition and evolution of Flex has been influenced by our incredibly talented developer community from day one,” said David Mendels, senior vice president, Enterprise and Developer Business Unit at Adobe. “The decision to open source Flex was a completely natural next step. I am incredibly excited to deeply collaborate with the developer community on Flex, and further fuel its momentum and innovation.”
Now, although this would normally make sense to me, it doesn’t make half as much sense as it would for Adobe to have open sourced the FlashPlayer, which is not a direct source of income for them. The Linux community has been shouting loud for this to happen for some time now (with good reason, imho). If Adobe doesn’t want to support the FlashPlayer on Linux (which I *completely* understand and even agree with to some degree), they should just open source the FlashPlayer. Doing so would allow the community to create a better Linux player, integrate Flash content into more unique /non-traditional applications on any OS, and generally increase the spread of Flash content across all media platforms. Then, the demand for the ability to create Flash content would increase. If Adobe maintained the control of the applications used to develop the content that is used by these players, this would definitely increase their revenue. This seems like more of a “natural next step” then releasing the Flex SDK.
I say this only to question the true motives for going open source, not just to be another complainer. It doesn’t seem a complete coincidence that Microsoft Silverlight was officially released 11 days ago. But regardless of their motives, I tip my hat to Adobe for this bold move. Like Josh Tynjala talked about, there is definitely the possibility that people will be able to develop Flex and perhaps even Flash content without need for Adobe products. And although they will still have a monopoly on their FlashPlayer, this is not the product that brings in money for them.
In any case, I do agree that this will “further fuel its momentum and innovation.” Which, not so coincidentally, will help Flash platform development to remain more attractive than Silverlight (unless Microsoft follows suit). Way to go Adobe.
There are a lot of good physics libraries and examples out there, such as the Fisix Engine, . But I just stumbled across a couple new ones: www.feedthehead.net, www.acrobots.net, and this monster truck demo (with source). Feedthehead has some fun easter eggs as well; for example, try pulling off the nose multiple times.
With the performance increases in AS3, new developments in Flash 3d and continual growth in the Flash physics world, I imagine the quantity and quality of flash games will start becoming the “new wave.”
My copy of Adobe Creative Suite 3 Web Premium arrived in the mail today! It contains the following major programs on a single DVD:
- Dreamweaver CS3
- Flash CS3 Professional
- Photoshop CS3 Extended
- Illustrator CS3
- Fireworks CS3
- Acrobat 8 Professional
- Contribute CS3
Needless to say, this image expresses how I felt when I opened the package:
However, after I finally got the installation process to complete almost *two hours* later, this image better expresses how I felt:
No problem, just use the Add/Remove Programs tool to get rid of Photoshop CS3 Public Beta. Oddly enough, the uninstaller completed successfully, but didn’t do a single thing! I did a quick web search to see if anyone else had this problem and discovered this Adobe tech note titled, “Remove Photoshop CS3 public beta.” It instructed me to use a cleanup script that Adobe had created for just such a problem. It came with a not-so-comforting little disclaimer:
“WARNING: Before running the CS3Clean Script, it is critical that you back up your hard drive including all data, content, and applications. Failure to do so, and failure to follow the instructions included with the script, could result loss of the contents of your hard drive.”
Are you kidding me!? I have to backup my *entire* hard drive in order to go from an Adobe Photoshop CS3 Beta to the real deal?? Did I just go back in time 10 years? And according to this Adobe forum post, the backed up data had to be on some media that was complete disconnected from the system!
…Well, to avoid the possibility of wanting to go postal on Adobe if I lost my data, I decided I would heed the advice, although I was not at all happy about it.
Once complete, I downloaded the Adobe cleanup script zip archive, but noticed it contained the following file:
I seriously thought there was a fair chance I was about to lose my entire hard drive by continuing with the process; but since I didn’t seem to have an alternative, I did it anyway.
I had to run the CS3Clean.exe script 3 times! The first time I discovered that I apparently didn’t have the “Windows Installer CleanUp Utility” installed on my system and had to download it here. After running the script the second time, it had an option to “preview” the changes that would occur without actually doing them–I thought this a wise idea so I went with it. Unfortunately, after the preview, the script shut down automatically and I had to start it back up from the beginning. It actually executed fully the third time, at “level 1” (whatever that meant). Finally, the CS3 installation began successfully–or so I thought. When installation finally completed, I got the following error:
No Illustrator CS3? And no reason?? I still haven’t figured out why there is a problem with this and the last time I called Adobe I had an hour wait time. Grrr…
Oh, and another screwup: when I started Flash CS3 there was an “Upgrade Check.” It automatically correctly determined that I was upgrading from CS 2.3 Premium and asked for me to enter the serial #. I entered the serial number and it said it was invalid. Apparently, I had to enter the serial number on my CS2 Premium case labeled “Acrobat Pro.”
Seriously, this is the worst install experience I’ve ever had. Setting up my Linux distro was easier than this. I’m usually a fan of Adobe, but this was a big mistake of theirs in my eyes. Jumping through all these hoops and problems is lame, but more importantly, no one’s hard drive should ever be at risk because they want to install an Adobe product.
Well, it was fairly easy to setup with two minor complications. First, the installation instructions said extract the zip file into the wp-content/plugins directory, upload, and wallah. However, extracted folder is called “iG-Syntax Hiliter” which contains the folder “ig_syntax hiliter plugin.” It is actually this second folder that needs to reside in the plugins folder in order to work. The second hiccup was that when I tried to activate the plugin, it was looking for the “ig_syntax_hilite” folder in the plugins directory as well. Unfortunately this folder was in the plugins/”ig_syntax hiliter plugin” folder so I had to copy it over.
After these changes, everything worked fine. It was easy to use and configure from the wp-admin–>Options screen. Here’s what the code looks like:
- import flash.display.Sprite;
- * Test class
- * @author Nate Chatellier
- public class MyTestClass extends Sprite
- protected var _bMyVariable:Boolean;
- * Constructs the MyTestClass object.
- * @param bMyVariable This is MY variable!
- public function MyTestClass(bMyVariable:Boolean)
- _bMyVariable = bMyVariable;
- } // END CONSTRUCTOR
- } // END CLASS MyTestClass
- } // END PACKAGE
I’m not the biggest fan with the default look. I may try to play with the settings or I may evaluate different highlighter options depending on whether I feel it’s needed. More soon…
So I was going to continue with the AS 3 network programming discussion and realized I don’t yet have any AS3 syntax highlighter setup. I’d hate to make you read code that didn’t look pretty ;). I was considering FlashTextEditor, dp.SyntaxHighlighter, or ig:Syntax Hiliter, although I think I’m leaning toward the latter. Any Suggestions?
I used MC Tween all the time. When AS 3.0 came out, I loved everything about it, except the fact that there was no MC Tween. I sent an e-mail to Zeh asking if he was planning on porting it. He mentioned that he was working on a new implementation called Tweener, but that it was only for AS 2 and not yet ready for the public. Zeh was kind enough to send me the source so I spent that week porting it to AS3.
It’s funny to think back to those days, which was really not that long ago, and now I couldn’t even imagine what it would be like to make a Flash app without using Tweener!
Well, apparently I’m not the only one. It’s been great to see John Grden talk about how easy it was to learn and use in conjunction with Papervision3D, or to hear Ricardo Cabello (aka, Mr. Doob) say, “If you need a tween class, don’t look [at] any other, you only need Tweener!”
If you don’t know what Tweener is yet, head over to the project on Google Code and read the quick description. I promise it is worth it!
A while back I led an ActionScript 2 project with two other classmates in a Network Programming class I was taking at EWU. We created a Flash game we called “Zelda Arena” in which multiple players on multiple computers would connect via TCP/IP to a C++ server. You could custom color your own hero (Link) and duke it out against each other with your sword. I wanted to take the idea a lot further, but figured Nintendo might not appreciate it. Here are a couple of screen shots:
If Nintendo happens to read this, I would love to build you this game for your website! A guarantee it would generate a lot of traffic. But alas, you will probably just steal my idea. Then again, I guess I stole yours first.
Anyway, I have since redone a lot of ActionScript 3 networking code. In the near future, I’ll post some further information on Flash/AS3 networking and share some free classes to simplify the process. Stay tuned…
I recently had reason to attempt to convert a few old ActionScript 2 classes to ActionScript 3. I stumbled across Patrick Mineault’s post about his AS2 to AS3 converter and decided to give it a whirl. It worked better than I had expected actually. I ran my code through the converter, spent about 10 minutes fixing a few small details that it didn’t catch and I was up and running–nice! It is certainly not a completely full proof converter but is nonetheless a great time saver. Unfortunately, Patrick is now “retired” so I doubt we will see any future updates, but it’s a great place to start.
The increase in performance and capabilities of ActionScript 3.0/Flash CS3 over AS2/Flash 8 is astounding! I intend to discuss this more in the future, but for those who haven’t yet made the jump and are considering it, I cannot possibly urge you strong enough to make the jump. Especially since Flash Player 9 is at 83.4% in the March 2007 penetration study! To make this transition a little easier, here are some useful links to start learning it:
Adobe Flash CS3 Quick Starts
ActionScript 3.0 brief overview
Adobe Flash CS3 Samples
Adobe Flash CS3 User Guid (pdf)
Flash CS3 Documentation
ActionScript 3.0 language reference
ActionScript 3.0 Cheatsheets
Migrating ActionScript 2.0 code to ActionScript 3.0
ActionScript 3.0 Tip of the Day
Getting Started with ActionScript 3.0… (by Senocular)
With a zillion Flash platform blogs already out there, why am I starting another one? Because I believe taking without giving is inherently wrong.
I started Flash development in Flash 4 and instantly fell in love with the platform. I’ve had my rants about it over the years, but for the most part, I’ve thoroughly enjoyed it. I was especially pleased when the Flash 9 Public Alpha was released. I downloaded it the same day and I literally have not used ActionScript 2.0 since.
The information sharing and open source Flash platform community has helped me tremendously over the years and I decided it’s finally time I start giving back. Aside from working on group effort projects, such as Tweener, I intend to post many experiments, utility classes, and tutorials on this site as well. Like John Grden’s claim, I’m not an OS activist that hates corporate work; but I do love the OS community and intend to play an active role in it. More to come soon…