Notice - June 10, 2009 -
Well, I know the AS3 Main hasn't had activity for a while, but there are probably still people using it as a learning resource, and since I've been learning AS3, why not share my knowledge? They're aren't an abundance of AS3 tutorials across the internet like you can find with AS2, so, I'll bring the tutorial count up.
Now on to the tutorial
AS3 : Opening a URL -
Finished Product (Requires Flash Player 9.0 or higher)
Newgrounds Graphics Sheet (used for button style)
Ok, so let's get down to business! Please set up your stage as follows:
Default 550x400
Frame Rate of 30
Two layers, one named "Actions" and the other "Button"
A button instance named "newgroundsButton" (no quotations) on the "Button" layer.
Ok, so if you don't know how to set up your stage, please look elsewhere, this is an Actionscript tutorial.
Now then:
Step 1 -
Open the Actions Panel (F9, or Right-Click>Actions) on your Actions Layer.
Step 2 -
Type in the following code (please type, Copy+Paste doesn't help you learn anything...)
var newgroundsURL:String = "http://www.newgrounds.com/";
var newgroundsURLRequest:URLRequest = new URLRequest(newgroundsURL);
function getNewgrounds(event:MouseEvent):void{
navigateToURL(newgroundsURLRequest);
}
newgroundsButton.addEventListener(MouseEvent.CLICK, getNewgrounds);
Now, the explanation:
Line One -
var newgroundsURL:String = "http://www.newgrounds.com/";
var - is the portion of this statement (a line of code is properly referred to as a statement) that lets flash know we are defining a variable.
newgroundsURL - is the name of our variable.
:String - this determines that our new variable is a String, which can store only text, in older AS2 projects, this was not needed, but it is now required for AS3, also it improves the speed of calculations.
= - Pretty self explanatory.
"http://www.newgrounds.com/" - this is the value of our String variable, a String's value is always in quotation marks.
Line Two -
var newgroundsURLRequest:URLRequest = new URLRequest(newgroundsURL);
This defines another variable, a URLRequest, which is basically a link. This link's value is listed as newgroundsURL, our String variable.
Lines Three to Five -
function getNewgrounds(event:MouseEvent):void{
navigateToURL(newgroundsURLRequest);
}
This code block defines a new function called getNewgrounds, the function is a MouseEvent, which means that it can be called from the viewer's mouse doing something - we have yet to define the something...
Line Six -
newgroundsButton.addEventListener(MouseEvent.CLICK, getNewgrounds);
This adds an EventListener to our button, an EventListener basically sits and waits for an event to happen, and preforms the function attached to it. Here, we have attached a CLICK event to our Button, the function is getNewgrounds, our function for linking to Newgrounds. So in English, when the button is clicked, Flash will open your internet browser and go to Newgrounds.
Final Notes -
Now, if you close out the Actions panel, and Hold Control(Command on Mac) then Press Enter(Return on Mac), you should see the .swf Preview of your work, and clicking the Newgrounds button will, OMG, go to Newgrounds.
Thank's for reading, good luck on future projects!
~Oldsage10~