Flash Actionscript 3 URL link button
What:
To make a button with a link to an external webpage using Actionscript 3
Example:
Click on the “alexbrooke.com” button in the top left corner to navigate away from this page
To find out how to do this go to the next page:
How:
1) Draw your button
2) Select your button and click on MODIFY then Convert to Symbol
3) Choose button from the menu and click OK
4) Select the button if it isn’t selected. Now give it an instance name of “urlButton” in the small dialog box in the bottom left hand corner
5) Create a new layer and insert the following actionscript into it:
function gotoHomePage(event:MouseEvent):void
{
var targetURL:URLRequest = new URLRequest(“http://yourhomepage.com/“);
navigateToURL(targetURL);
}
urlButton.addEventListener(MouseEvent.CLICK, gotoHomePage);
6) Change the red url to whatever website you want to navigate to.
7) Publish the movie and see if it works
Extra stuff
The video player I used for this example was made by Rafael Nuenlist. You can view the tutorial here.
Please leave your comments, ideas and questions here.
Finally a straight-forward tutorial without any hitch and without any code problem. It works. I checked all the other tutorials for url buttons in AS3 and they ALL had some kind of problem: something assumed, missing code, incorrect code, etc.
Thanks you
Well, basically, these tutorials are for me which means they don’t assume you know anything about actionscript or flash. Glad you liked the post
Hi, thank you for your script, just one question, when trying it locally it gives the hand indicator for a link, but when trying from the actual site I placed the swf, my button doesn’t show me the hand indicator. any ideas? thank you.
Hmm… strange. Got the URL and code?
Hi…perfect code, but how do I get it to target=”_self”? It seems to be defaulting to open a new window. I need it to remain in the same window. When I add “, “_self” I get an error saying there are too many functions
Try adding “_self” to the navigateToUrl function. Like this;
function gotoHomePage(event:MouseEvent):void
{
var targetURL:URLRequest = new URLRequest(“http://yourhomepage.com/”);
navigateToURL(targetURL,”_self”);
}
urlButton.addEventListener(MouseEvent.CLICK, gotoHomePage);
thanks mate ! that really worked! !
AHH i can’t get anything from any site to work.
1120: Access of undefined property urlButton.
I don’t know what i’m doing wrong, but i followed your directions to the T, however i think
4) Select the button if it isn’t selected. Now give it an instance name of “urlButton” in the small dialog box in the bottom left hand corner
Might be getting me messed up. I don’t see anything “instance name”, i just see Name: Type: …. then down below it has “Linkage/ checkbox for Export for actionscript, etc and then class stuff… I don’t see anything about “instance name”…
Oh please help, i’m going nuts over this! haha
I just don’t get it… Can’t believe that made it so dang stupid!
@neosin
OK are you using CS3 or CS4? There should be a small blank dialog box where you can enter “urlButton” on the bottom left. Looks like this: http://www.vineyardesigns.com/images/resources/flash_as/button_name.gif
In CS4, the blank dialog box is in the top right. Do you have all the necessary windows open? If not, go to WINDOW then WORKSPACES and then click on “Default” in CS3 or “Essentials” in CS4.
Hope that works!
Hi all, I’m new to flash so hopefully someone can please help me out.. Using Flash CS4 with Actionscript 3.0 and it seems to me that many people are having issues with it?
I’ve created a movie clip and added a button on the last keyframe (so that the button doesn’t appear until the end).
I have added the following code to the last keyframe (when the button appears)
enterbutton.addEventListener(MouseEvent.CLICK, onMouseClick);
function onMouseClick(event:MouseEvent):void
{
var request:URLRequest = new URLRequest (”http://www.adobe.com”);
navigateToURL(new URLRequest(”http://www.adobe.com”), “_blank”);
}
Problem is, the little hand icon appears when i hover it over the button, but nothing happens when I click.. Been trying out all different codes but nothing works and I’m getting pretty frustrated now..
I’m sure there’s a Flash Guru who can help me out! =) Thanks in advance!
I think I’d need to see the file before I could help you.
could i please send you the file in that case?
hey, I’m still new at this. how can I code this to link my buttons to Program Files on my computer, and is it any way to have the buttons put on my desktop for easy access?
@Vestige
Please send me a file
@mike
You need the location of the file on your computer. Are you using a mac or PC?
I’ve been all over the internet and nothing seems to work. I don’t know enough to figure this out on my own so hopefully someone can help. I’m using Flash CS4 with Actionscript 3. What I’ve set up is five different buttons and each of them is supposed to take you to a different part of the website; you can probably tell yourself because of the names. Everything works OK except for the fact that I can’t get these links to open up inside the same window. What am I missing?
//SETUP
import flash.net.URLRequest;
import flash.events.MouseEvent;
var GET_HOME:URLRequest = new URLRequest(“http://www.website.com”);
var GET_INSPIRATIONAL_GALLERY:URLRequest = new URLRequest(“http://www.website.com”);
var GET_SERVICES:URLRequest = new URLRequest(“http://www.website.com”);
var GET_ABOUT_US:URLRequest = new URLRequest(“http://www.website.com”);
var GET_CONTACT_INFORMATION:URLRequest = new URLRequest(“http://www.website.com”);
//HOME
home_btn.addEventListener(MouseEvent.CLICK,GET_HOME_ACTIVATOR);
function GET_HOME_ACTIVATOR(event:MouseEvent):void{
navigateToURL(GET_HOME);
}
//INSPIRATIONAL GALLERY
inspirational_gallery_btn.addEventListener(MouseEvent.CLICK,GET_INSPIRATIONAL_GALLERY_ACTIVATOR);
function GET_INSPIRATIONAL_GALLERY_ACTIVATOR(event:MouseEvent):void{
navigateToURL(GET_INSPIRATIONAL_GALLERY);
}
//SERVICES
services_btn.addEventListener(MouseEvent.CLICK,GET_SERVICES_ACTIVATOR);
function GET_SERVICES_ACTIVATOR(event:MouseEvent):void{
navigateToURL(GET_SERVICES);
}
//ABOUT US
about_us_btn.addEventListener(MouseEvent.CLICK,GET_ABOUT_US_ACTIVATOR);
function GET_ABOUT_US_ACTIVATOR(event:MouseEvent):void{
navigateToURL(GET_ABOUT_US);
}
//CONTACT INFORMATION
contact_information_btn.addEventListener(MouseEvent.CLICK,GET_CONTACT_INFORMATION_ACTIVATOR);
function GET_CONTACT_INFORMATION_ACTIVATOR(event:MouseEvent):void{
navigateToURL(GET_CONTACT_INFORMATION);
}
I spent 3 hours going site to site trying to figure this out. Other site have NO CLUE what the hell there talking about all the codes they give out FAIL over and over, and yours worked right off the bat.
Thank you for knowing what your talking about & sharing.
Live . Love . Laugh
~Reza Evol~
Thanks.
Hello everybody!
I have a serious problem with a code.
This is my code:
function makebtn(mc:MovieClip,aurl:String):void{
mc.aurl = aurl;
mc.buttonMode= true;
mc.addEventListener(MouseEvent.CLICK,gotoaurl);
}
function gotoaurl(e:MouseEvent):void{
var aurl:String = e.target.aurl;
navigateToURL(new URLRequest(aurl));
}
makebtn(btn1,’http://www.google.com‘);
|
I have tried all codes to make it work for a simple button or movieclip have the task to open an URL in _blank window. This code wotks just fine, but when i put the swf in dreamweaver and publish in html, the code for the just simple doesn´t work!
The button and code is split into two windows (content), 1, Scene 1 -> ScrollContent -> content.
The buttons placed outside the ScrollContent with the same codes work. But the others in content area (wich have the scroll) doesn´t works!
Can anyone help me? please!
This is the best site I have ever found. Thank you for providing straight forward answers and info.
Great tutorial. This is the only one I found that makes sense!
THANK YOU!!!!!!!!! You have no idea how desperate I was to find this help. THANK YOU!
thank you it works
Thank you so much for your help, I searched for ages for some code like this, and it works perfectly.
BTW, are there any other flash tutorials you would like to see?
Very good, clear, logical tutorial, the best I found online! THX
Thank you so much !!!
i have a problem with a button in flash cs4.Its working in flash but not on the browser
Hi,
I’m having the same problem as Vestige. I have an animation that I’ve created where the button comes in at the end.
When I put the code in the animation doen’t play at all. It doesn’t give me any compiler errors it just says the following under output:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Banner01_fla::MainTimeline/frame1()
If I put the button in frame 1 everything works perfectly. However as soon as I move it to another frame (ie. later in the animation) it stops working.
Do I need to the EventListener to point somehow to the specific frame where the button comes in??
How do I code that??
Is flash that ridiculous where people have to search high and low for a tutorial on how to create a simple link? The program should be canned and filed away in a government warehouse somewhere never to be seen again
Just as a suggestion for those of you using the code above.
My past experiences always told me that when you copy and paste the code directly into the action script panel, you may get syntax errors when publishing. Do not do this. Sometimes the pasting from the site will add an improper space or break the code in general.
Best is to type the code out exactly with the proper beginning and end brackets. Test it out and see if it works. Hope this helps.
I sense a little frustration here. The problem with computers and programming languages is they are created by people who know how to use them. Then there’s the rest of us…
hi..gr8 tutorials! thanks
i am developing a video player similar to YouTube. i need to add EMBED option in that so that my video can b shared on other sites like Facebook. (please refer to you tube Share/Embed option if i am not being clear here). On clicking Embed Option(button) a pop up box should appear which will give the HTML code. that link if pasted elsewhere should add that video in that page n play there(similar to YouTube Embed features) . Please let me know.
the flash video player is built using Flash CS3 ,Actionscript 2.0.
how to open a popup in same stage. On stage has button, when click the button then open the popup. Is it make Flash Action Script 3.0
How to open a popup in same stage. On stage has button, when click the button then open the popup. Is it make Flash Action Script 3.0
Hi i have designed the home page i need to give link for next button like AboutUs this script is not working canu please help me out thank you
thx mate..its really work for me..
Great, glad to hear it. Thanks for checking out the site.
Cheers for a CLEAR & SIMPLE tutorial on creating a link from a button. Much respect, appreciate it!
Glad you liked it. Thanks for checking out the site.
Hi!
Thanks for the code. My only problem is that there’s an AS3 glowing effect in the code as well, and if it is running, it gets to the top of the area (the link do not works until it is over).
Have you got any ideas how to solve it?
Thanks again!
If you get the error “Syntax error: expecting rightparen before rightbrace” it’s because you copied and pasted the code.
The error for me was fixed after i deleted the both the ( and “ symbols on the 3rd line in URLRequest(“http://yourhomepage.com/“); i.e. the opening bracket and quotation mark.
Entering them in manually worked a treat. FYI i’m using Firefox to copy the code.
Thanks for the code Alex – this guide is simple and to the point. Top marks for you sir.
As a side note/rant; AS3 is awful, I can’t wait for Flash to be scrapped completely, Adobe only have themselves and their awfully complex programs to blame…It shouldn’t be this hard to create a hyperlink!!