Posted on January 17, 2008
Moving From Flash AS2 To Flash AS3: GetURL()
(Note: I’m converting my Flash game “Spin City” from AS2 to AS3 so it can be re-skinned for another project. I’ve decided to document this activity in a series of tutorials so they might benefit anyone else who is going through the same development process)
One of the most basic functions in AS1 & AS2 was getURL() . The simple ability to send users to another page in the same browser window ( or a new instance of one) is also one of the very foundations of web functionality. AS1 and AS2 used a very simple implementation for it, and we were all happy because it was all so simple. The basic, global function call looked like this
getURL(“catalog.aspx”, “seetoys”, “GET”);
The three parameters were:
- URL (where to go)
- target (the name of browser to open the page into)
- method (GET or POST)
That was it. Simple, easy, straightforward, nobody got hurt.
In AS3 there is no global getURL() function, there is no easy, one line equivalent that you can replace it with. A simple getURL() style action takes a 4 lines of code (2 lines if you want to minimize your code, but make it unreadable). Here are the steps you need to take to simulate getURL()
- Everything required for URL actions is AS3 is contained in the flash.net package. So the first thing you need to do is import that package into your class.
- Second, you need to create an instance of a URLRequest object and set it’s url property
- Third you must use the new navigateToURL() function is a close approximation to to getURL();
var url:String = “catalog.aspx”;
Of course, you could economize that code like this and make it one line (plus the import)
navigateToURL(new URLRequest(“catalog.aspx”), ‘seetoys’);
URLRequest is actually a very useful class, as it allows you to so things that were never possible in AS2, like set HTTP Headers for the request. Still, it’s a bit disconcerting when something so simple and basic as getURL() needs to be updated along with everything else. However, that seems to be the norm with converting AS2 to AS3.