17 January 2010

getURL in ActionScript 3 (navigateToURL)

One of the things that has become something of a pain in AS3 is getURL. If you’ve used ActionScript 2 before, you know how simple it is to open a URL in a web browser.

getURL("http://www.google.com");

In AS3, however, it gets a bit more complicated.
You have to declare your URLRequest. This is pretty much a fancy way of holding the URL. You may wonder, why you need a URLRequest to hold the URL when you could just use the string itself? Well the answer is because you can use the same URLRequest with many different functions. And there are special properties to a URLRequest.
This is how you declare a URLRequest:

var url:URLRequest = new URLRequest("http://kornesh92.blogspot.com");
The URLRequest by itself is useless, but coupled with navigateToURL, it’ll act like getURL.

This is the full code:
var url:URLRequest = new URLRequest("http://kornesh92.blogspot.com");
navigateToURL(url);
Put that in the first frame of your movie and when you compile it, this blog should open up in your browser.

1 comment:

  1. I last worked with Flash when it was ActionScript2, and your explanation seems sensible except for "put that in the first frame of your movie." I am used to putting "getURL and stop" in the LAST frame of an intro movie, to allow the real main page to load. Is the "stop" part now unnecessary, as long as "0 repeats" is selected? And what about "_self" ? Please advise. Many thanks!

    ReplyDelete