Tuesday, April 14, 2009

Link a url in AS3.0

extension of the old post if you have not read the old one follow the link
button event in AS3.0

In this one i like to explain how to link a url in AS3.0
Step 1:
Create a button with the event and the function. if you are not aware plz read the previous post button event in AS3.0

Step 2:
inside the function write the code to link the URL
You need to create a URLRequest object in the Syntax
var <url_oblect>:URLRequest = new URLRequest(<url string>);

in our example the code will look like
var url:URLRequest = new URLRequest("http://gbala.com/");
Where url is the object and  http://gbala.com/ is my website url
once it is done

Step 3:
ask the url to load the site.
navigateToURL(<url string>);
in our example
navigateToURL(url);
Where this line ask to go to the url specified in the URLRequest.

where navigateToURL can have 2 parameter and the second one can be either of this 4 
"_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.


The whole code is as followes...

function button_click(event:MouseEvent):void
{
        var url:URLRequest = new URLRequest("http://gbala.com/");
        navigateToURL(url);
}

b1.addEventListener(MouseEvent.CLICK, button_click);

in AS2.0 it is just one line code getURL("url");
but in AS3.0 they changed a bit.

Download source code file : Navigate.zip

Do u want to search anything in my site just try out the Google search Above next to the head.
Don't forget to leave a comment about this

9 comments:

Eu said...

Hey,
After I did some reader in your post, I tried to do this:
http://blog.gbala.com/2009/04/as30-button-event.html
and it worked, but when I tried to get it to go to an URL, it simply didn't.
I've done everything you did. my button is called b1 and his instance name is b1, the function is just like you did.
Here is a Screen Shot of the errors:
http://i39.tinypic.com/1eattf.jpg

Please help me, this is urgent and it's for a work.

Bala said...

Hi,
I Noticed in your image that the function is not written.

like
function button_click(event:MouseEvent):void
{
trace("button is clicked");
}

put your code inside this...

Bala said...

Hi,
Also Check this below link to get a clear ides...

http://blog.gbala.com/2009/04/link-url-in-as30.html

Eu said...

Ok, now my script is:

function button_click(event:MouseEvent):void
{
trace("button is clicked");
}

b1.addEventListener(MouseEvent.CLICK, button_click);
URLRequest = new URLRequest("http://google.com")
navigateToURL(url)

but then still getting errors:
1067: Implicit coercion of a value of type flash.net:URLRequest to an unrelated type Class.
1188: Illegal assignment to class URLRequest.
1120: Access of undefined property url.

Sorry for being such a noobie. Gotta start somewhere, eh?

Bala said...
This comment has been removed by the author.
Bala said...

Hi,
Added the source file in the post http://blog.gbala.com/2009/04/link-url-in-as30.html so you and download from there and compare the code.
Hear is the source code link...
www.gbala.com/grabmycode/fla/Navigate.zip

Anonymous said...
This comment has been removed by a blog administrator.
rahul said...

done a great job,nice articals
http://www.learntohack.webnode.com

Anonymous said...

Hi,
Big thanks to your tutorial. I have a problem where I need to insert a clickTag into my script.

In AS2.0 :
on(release) {
getURL (_level0.clickTag, "_blank");
}

How can I have the same clickTag in AS3.0?

Thanks in advance!

Sean