When I first started working with Actionscript 3, I had a list of things I do in Actionscript 2 that I had to be able to do in the new language. Near the top of my list was creating hyperlinks.
In Actionscript 2 creating a hyperlink was one line of code.
getURL("http://www.somesite.com");
place that code somewhere when you wanted to go to a URL and it would work like a charm. If I use it in the MovieClip from my previous post about Events it would look like this...
myClip_mc.onRelease = function():Void
{
getURL("http://www.gotoandlearn.com");
}
In Actionscript 3, the process is only a little more complicated. (once you get past the Event differences)
The new code is in blue and bold.
for an explanation of the older code see this post... AS3 Event Basics
var myURL:String = "http://www.gotoandlearn.com";
var myRequest:URLRequest = new URLRequest(myURL);
myMovieClip_mc.buttonMode = true;
myMovieClip_mc.addEventListener(MouseEvent.CLICK, doOnClick);
function doOnClick(e:MouseEvent):void
{
navigateToURL(myRequest);
}
The first new line of code...
var myURL:String = "http://www.gotoandlearn.com";..creates a named variable to store our URL address. It is not necessary to store this as a separate variable, but in the long run it is the better practice. I have found that is usually saves me typing, makes my code easier to read, and allows me to change or delete the value more easily.
The next line...
var myRequest:URLRequest = new URLRequest(myURL);..creates a new URLRequest object in the variable myRequest and passes to the new object the myURL variable we created in the previous line. This code does not create the hyperlink itself. Think of this as just packaging the information about the hyperlink so it is ready for the next piece. There's much more to a hyperlink than just the URL. There are things like the contentType, the method, and the requestHeaders. While all we need for this example is the url, the folks who put together the URLRequest want to give advanced developers more tools to work with.
The last new line...
navigateToURL(myRequest);...tells the browser to go to the URL (and send along any extra information packaged in the myRequest variable).
That's all we need to have a button or a MovieClip link to a location in the browser. While the basic code may be more than in Actionscript 2, it is set up so advanced developers have more options and the same URLRequest class (object type) can be used in various ways throughout Actionscript.
Speaking of which... I think that will be the topic of an upcoming post. I'll show the basics of using the new Loader class to load external images or swf files.
'till then,
--Rich
[Shameless plug]
Oh, by the way... this is one of the many topics covered in the introductory level Adobe Flash CS3 class at Ascend Training in Deerfield, Illinois and Chicago, Illinois. For the current schedule, go to http://www.ascendtraining.com/schedule.php and click on the name of the class to see it's outline.
No comments:
Post a Comment