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)

 

Flash AS2

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

[cc lang=”javascript” width=”550″]
getURL(“catalog.aspx”, “seetoys”, “GET”);
[/cc]

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.

Flash AS3

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()

  1. 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.
  2. Second, you need to create an instance of a URLRequest object and set it’s url property
  3. Third you must use the new navigateToURL() function is a close approximation to to getURL();

 

[cc lang=”javascript” width=”550″]
import flash.net.*;

var url:String = “catalog.aspx”;
var req:URLRequest = new URLRequest(url);
navigateToURL(req, ‘seetoys’);
[/cc]

 

Of course, you could economize that code like this and make it one line (plus the import)

 

[cc lang=”javascript” width=”550″]
import flash.net.*;

navigateToURL(new URLRequest(“catalog.aspx”), ‘seetoys’);
[/cc]

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.

Leave a Reply