00:00
00:00
Newgrounds Background Image Theme

YubaOfficial just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

As3:main - Opening A Url

2,229 Views | 2 Replies
New Topic Respond to this Topic

As3:main - Opening A Url 2009-06-10 18:18:55


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:Main

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~

Response to As3:main - Opening A Url 2009-06-10 18:32:47


You can also specify where the new window should open:

navigateToURL(newgroundsURLRequest, "_self" );

"_self" specifies the current frame in the current window.
"_blank" specifies a new window.
"_parent" specifies the parent of the current frame.
"_top" specifies the top-level frame in the current window.

Also, if you're feeling lazy you can define the URLRequest while calling navigateToURL:

navigateToURL( new URLRequest("http://www.google.com"), "_self" );

Good tut.


The water in Majorca don't taste like what it oughta.

| AS3: Main | AS2: Main | Flash Tutorials |

BBS Signature

Response to As3:main - Opening A Url 2009-06-10 18:34:02


Oh, thanks.

Well, I'm quite new to AS3 myself, been self learning things from the Flash Help for about a week now...