…and especially enthusiastic about motion tweens in Flash…
One of my frequently asked questions in the comments is how to control what window a button click opens using ActionScript 3.0. For example, you might want your button to open in the same window, a particular part of your frames layout, or a new window. It’s similar to ActionScript 2.0, but there is a bit of a difference.
In ActionScript 2.0, you set a target for your button using code such as the following:
The following steps for ActionScript 3.0 will apply to either Flash CS3 or Flash CS4, just make sure that your document is an ActionScript 3.0 button and you’re adding the code to a frame.
my_btn.my_btn.addEventListener(MouseEvent.MOUSE_DOWN, myHandler); function myHandler(event:MouseEvent):void { navigateToURL(new URLRequest("http://www.adobe.com/devnet/flash/"), "_self"); }
By default, without adding this parameter, ActionScript assumes _blank which opens a new window. You can modify the _self in the code above to be _top or _parent (or _blank if you want). Here’s what each of them does:
_self opens the link in the current frame of the current browser window._blank opens the link in a new window (or tab if that’s what the user has their browser set to do)._parent opens the link in the parent of the current frame._top opens the link in the top-level frame of the current browser window.This blog specializes in the new motion model of Flash CS4, and is brought to you by team tween of Flash at Adobe. The blog will includes tips, techniques, and info on tweening in Flash using the Timeline and Motion Editor (as well as some stuff about motion runtime with code). From time to time, we might also talk about ActionScript 3.0. So if you're interested in tweens, this blog is for you. We also want to hear your feedback in the comments. All views expressed here are those of the authors, and do not necessarily represent the opinion or views of Adobe Systems Inc.

Lucia
August 3rd, 2009 at 6:30 pm
okay what if I just want a script that would work on MKS to work here? like this one that would lead to another frame:
on (release) {
_root.gotoAndPlay(”");
}
Jen deHaan
August 7th, 2009 at 9:37 am
@Lucia: Not to sure what the MKS is, but your code is ActionScript 2.0 that should be placed on a button (select the button and add the code to it.) If you want to use the equivalent ActionScript 3.0 to do that, please see this post: http://flashthusiast.com/2008/02/25/making-a-button-work-in-flash-cs3-with-actionscript-30-its-not-too-bad
Mary Ann
August 19th, 2009 at 6:32 am
Hi Jen,
I’m creating my first Flash website in CS4. I have what I think is called a splash page, with an enter button to navigate to the home page. I have an actionscript/label layer, and all the pages have frame labels. The file works great, except I can’t figure out how to make the enter button on the splash page link to the home page.
I’ve tried using the same invisible button I used for the pages on the menu bar, with similar code, but it’s not working.
How do you link a button in a Flash website to a different page in the same website?
Thank you,
Mary Ann
TomA
August 19th, 2009 at 7:30 pm
How do I link a button on MouseOver to change the frame on another movieclip on the stage?
Basically I want to change the background image on a rollover for each button with AS3.
Alan
August 26th, 2009 at 8:41 pm
It works fine with one button, but when I try it with two buttons I get this error:
(Location) Scene 1, Layer ‘Layer 2′, Frame 1, Line 2
(Description)1021 Duplicate function definition (Description)
(Source) (function myHandler(event:MouseEvent:void{
Remco
September 6th, 2009 at 2:37 am
Alan, you have to rename the Myhandler function for every button.
So button 1 has myHandler
and button 2 uses myHandler2
Those names may not be the same!
Schrene
September 9th, 2009 at 6:23 am
I am new to Flash. I have built websites and dynamic PDF’s so I am familiar with “coding”.
I can not figure out why in AS3 I have to have my buttons on all of the pages in order for them to work. It seems some of the simplest things are what end up stumping me.
I know how to make this work with AS2 but I am more interested in going forward with AS3.
Any help would be VERY appreciated.
:)
Schrene
September 9th, 2009 at 6:47 am
I knew it was easy!
I just figured it out on my own.
Thanks for the help LOL
Bret
September 9th, 2009 at 9:27 am
My “_self” command is not being recognized or is being interpreted as “_blank” as each button opens a new page instead of the requested URL into the existing window.
No errors reported and the links open just fine,… only in a new window every time.
Could it possibly be because I’m trying to navigate to a another page within the same directory instead of a full URL?
Here is a sample code:
function goTeamMembersPage(event:MouseEvent):void {
navigateToURL(new URLRequest(”teammembers.html”), “self”);
}
History.addEventListener(MouseEvent.MOUSE_UP, goHistoryPage);
Bret
September 9th, 2009 at 9:30 am
Nuts. Copied the wrong segment of code. Should have been this:
TeamMembers.addEventListener(MouseEvent.MOUSE_UP, goTeamMembersPage);
function goTeamMembersPage(event:MouseEvent):void {
navigateToURL(new URLRequest(”teammembers.html”), “self”);
}
Sue Confused
September 9th, 2009 at 5:50 pm
I hate coding….am so used to desktop authoring that I’m spoiled rotten. All I’m trying to do is incorporate a back, next, and exit button but can’t seem to get the action script written correctly. Can someone help?? Thanks so much.
mehdi
September 12th, 2009 at 3:07 am
Want vision video
riad
September 16th, 2009 at 2:18 pm
i’m sending you this message and asking your help in something a bit easy for you,
if you log in to this website: http://www.bay-tower.com
you’ll see how the menu and the associated pages is working: moving forward, when clicking another it goes backward and the clicked one forward etc…
how can we do it with minimum effort plz,
waiting your reply
Regards
Mary
September 20th, 2009 at 6:56 pm
Hi,
No matter what I can not stop my flash file from looping. It seems like the simplest command. I’ve tried “stop();” “this.stop()” “goToandStop();” This is extremely frustrating. Any ideas why this won’t work?
Thanks,
Mary
matilda
September 22nd, 2009 at 1:10 pm
HELP!!!
PLEASE:)
Hello,
I need help with some actionscript!;)
my slideshow is almost workin but i am getting an error message saying:
1084: Syntax error: expecting rightparen before SimpleButton.
Here is what my code looks like:
stop();
//next button
SimpleButton.addEventListener(next_btn.onPress=function(){
if(studio_art1._currentframe==studio_art1._totalframes){
studio_art1.gotoAndStop(1)
}else{
studio_art1.nextFrame()
}
}
//last button
SimpleButton.addEventListener(back_btn.onPress=function(){
if(studio_art1._currentframe==1){
studio_art1.gotoAndStop(studio_art1._totalframes)
}else{
studio_art1.prevFrame()
}
}
Diipole
October 15th, 2009 at 6:51 pm
Thanks for that! Finally the right info.
Eleya
October 19th, 2009 at 10:59 pm
Okay, sorry for my messy msg but I really do need help.. HELP! I found that nice free template I’d love to use:
http://www.flashmo.com/preview/flashmo_132_horizon
Can I add “getURL” buttons (more like menu) to this layout via CS3/CS4? I have products I’d like to show like this and I have ~5 different categories. :) Anyone can help me?
THE CODE:
stop();
import fl.transitions.Tween;
import fl.transitions.easing.*;
var filename_list = new Array();
var url_list = new Array();
var url_target_list:Array = new Array();
var title_list = new Array();
var description_list = new Array();
var i:Number;
var tn:Number = 0;
var scale_factor:Number = 0.65;
var rollover_duration:Number = 0.4;
var motion_duration:Number = 0.8;
var dragger_tween:Tween;
var half_of_tn_width:Number = 80;
var new_position:Number;
var total:Number;
var flashmo_xml:XML = new XML();
var folder:String = “thumbnails/”;
var xml_loader:URLLoader = new URLLoader();
xml_loader.load(new URLRequest(”flashmo_129_thumbnail_list.xml”));
xml_loader.addEventListener(Event.COMPLETE, create_thumbnail);
var thumbnail_group:MovieClip = new MovieClip();
stage.addChild(thumbnail_group);
thumbnail_group.mask = tn_group_mask;
thumbnail_group.x = tn_group.x;
thumbnail_group.y = tn_group.y;
tn_group.visible = false;
tn_title.text = “”;
tn_desc.text = “”;
tn_url.text = “”;
function create_thumbnail(e:Event):void
{
flashmo_xml = XML(e.target.data);
total = flashmo_xml.thumbnail.length();
for( i = 0; i < total; i++ )
{
filename_list.push( flashmo_xml.thumbnail[i].@filename.toString() );
url_list.push( flashmo_xml.thumbnail[i].@url.toString() );
url_target_list.push( flashmo_xml.thumbnail[i].@target.toString() );
title_list.push( flashmo_xml.thumbnail[i].@title.toString() );
description_list.push( flashmo_xml.thumbnail[i].@description.toString() );
}
load_tn();
}
function load_tn():void
{
var pic_request:URLRequest = new URLRequest( folder + filename_list[tn] );
var pic_loader:Loader = new Loader();
pic_loader.load(pic_request);
pic_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, on_loaded);
tn++;
}
function on_loaded(e:Event):void
{
if( tn tn_group_mask.x + half_of_tn_width )
new_position = tn_group_mask.x + half_of_tn_width;
else if( new_position < tn_group_mask.x - thumbnail_group.width + tn_group_mask.width )
new_position = tn_group_mask.x - thumbnail_group.width + tn_group_mask.width;
new Tween( thumbnail_group, “x”, Strong.easeOut,
thumbnail_group.x, new_position, motion_duration, true );
}
var on_drag:Boolean;
var diff:Number = ( drag_area.width - dragger.width ) * 0.5;
dragger.x = drag_area.x;
dragger.y = drag_area.y;
dragger.addEventListener( MouseEvent.MOUSE_DOWN, drag);
dragger.addEventListener( MouseEvent.MOUSE_UP, drop);
dragger.addEventListener( MouseEvent.MOUSE_OUT, drop);
function drag(me:MouseEvent):void
{
on_drag = true;
if( dragger_tween )
dragger_tween.stop();
dragger.addEventListener( Event.ENTER_FRAME, update);
}
function drop(me:MouseEvent):void
{
on_drag = false;
dragger_tween = new Tween(dragger, “x”, Strong.easeOut, dragger.x, drag_area.x, motion_duration, true);
}
function update(e:Event):void
{
if( on_drag )
{
dragger.x = stage.mouseX;
if( dragger.x - drag_area.x diff )
dragger.x = diff + drag_area.x;
}
else if( dragger.x - drag_area.x == 0 )
{
dragger.removeEventListener( Event.ENTER_FRAME, update);
}
thumbnail_group.x -= ( dragger.x - drag_area.x ) * 0.6;
if( thumbnail_group.x > tn_group_mask.x + half_of_tn_width )
thumbnail_group.x = tn_group_mask.x + half_of_tn_width;
else if( thumbnail_group.x < tn_group_mask.x - thumbnail_group.width + tn_group_mask.width )
thumbnail_group.x = tn_group_mask.x - thumbnail_group.width + tn_group_mask.width;
}
Dave
October 20th, 2009 at 6:36 am
I have file that stops at a spot in the flash that has 5 buttons those 5 buttons go to 5 different frames (that also have stops) those frames have a button that links to a url I tried the code supplied here…
my_btn.addEventListener(MouseEvent.MOUSE_DOWN, myHandler);
function myHandler(event:MouseEvent):void {
navigateToURL(new URLRequest(”http://www.adobe.com/devnet/flash/”), “_self”);
}
but when I duplicate it for the other four buttons I get an error
Description: 1021: Duplicate function definition, Source: function mousedownhandler(even:MouseEvent):void{
Anyone got a solution?
Jared Schwalbe
October 20th, 2009 at 12:04 pm
Alright, I’m working on making a button open an html page in the same window. I’m using CS3 and actionscript 3 and this is my code:
my_btn.addEventListener(MouseEvent.MOUSE_DOWN, myHandler);
function myHandler(event:MouseEvent):void {
navigateToURL(new URLRequest(”http://www.adobe.com/devnet/flash/”), “_self”);
}
Now when I use it in the actual Flash file I want it in, I get no error message but it doesn’t work. However, when I open a new file and try it, it magically works. I’m so confused! Help?
Jared Schwalbe
October 20th, 2009 at 12:36 pm
I figured it out, I just needed to put it in the main timeline. However this did propose a new problem. When it’s in the browser and I click on the button that should take me to a url, a pop window pops up and says “adobe flash player has stopped a potentially threatening problem.” and goes on about how it’s trying to communicate with the internet and that I need to change my settings. I don’t want user to see this, so how can I fix it?
Cat
November 18th, 2009 at 1:48 pm
Thanks! was able to get my button to work!
Kurt
December 22nd, 2009 at 2:40 pm
I am a Noob to Flash and am having problems getting some movieclips to link to different URLs on release. I am able to get them to animate, and I am able to get them to link to an html page in the same folder, but I can’t get both functionalities to work at the same time and it is driving me crazy!! I’ve tried putting the actionscript in the instance object, in the action layer, and other things and nothing seems to work!?
Here is the link to the source file: http://www.myfavorites.com/flashtest/header_v8_122209_v2.fla
I believe that the actionscripting needs to be done in the “baners” movieclip, but it could also be the “Symbol 3″ movieclip. I’m not sure and this is where I also need help.
Here is also a link to the .swf (http://www.myfavorites.com/flashtest/header_v8_122209_v2.swf). See, if you mouse over the first two clips, they animate, but they do not go to the HTML page if you click on them (http://www.myfavorites.com/flashtest/success1.html). The third clip will go to the page success1.html in the same folder if you click on it, but it does not animate on mouse over.
I’m using Flash CS3 as my editor and I’m using ActionScript 2.0 for the scripting.
Any help you can provide on this is most appreciated!! I’m normally pretty good at figuring out things on my own, but This problem is driving me crazy!!
Farhan
January 7th, 2010 at 2:26 am
Hi,
I am new to AS3, the problem i am facing is when i click on the frame the “action panel” where we add the coding - it works fine, but when i add or make a button the “action panel” is not working
i can’t add any coding in it, like it gets disabled.
why does this happens.
Thanks
cassia
January 8th, 2010 at 2:11 am
Working well thanks
Jake
January 10th, 2010 at 8:23 am
can someone pls pls help me with linking my button to open in the same window. it is soooooo weird because with my first button (menu) it works but with my second, it doesnt work although every code is the same. it only works with _blank. with _self, if u click the button, nothing happens! nothing appears!/ i named my instance, put my actionscript in a different layer in the same frame, put
my_btn.addEventListener(MouseEvent.MOUSE_DOWN, myHandler);
function myHandler(event:MouseEvent):void {
navigateToURL(new URLRequest(”http://www.adobe.com/devnet/flash/”), “_self”);
}
I made one/ im trying to make another one. but it doesnt work !!! im so frustrated by it. pls help me solve this problem!~
gabby
January 28th, 2010 at 2:07 pm
please help!
I am making a flash portfolio site and i have many different buttons. i want them all the open on “_self”, but even though i renamed the handler function for each button, all the functionality stops working and clicking anywhere on the screen will only take you to the last buttons’ URL! If anyone knows why…please reply.
James Gray-King
February 9th, 2010 at 8:54 am
Hi all, some help would be gratefully received! I have used the code
fraud_btn.addEventListener(MouseEvent.MOUSE_DOWN, myHandler);
function myHandler(event:MouseEvent):void {
navigateToURL(new URLRequest(”http://www.fidelitypayment.com/node/25″), “_parent”);
}
and it is working fine when i test the movie. However, when uploaded to http://www.fidelitypayment.com/node/85 (my testing space) it ceases to work at all, any ideas?
Many thanks!!!
Kim - graphic designer and web designer
February 9th, 2010 at 1:53 pm
Hi,
I’m trying to make 4 different tweened buttons in a flash banner link to 4 different urls on a website. I can make the first one work, but this is the error message that I’m getting on the second one.
Attemping to launch and connect to Player using URL /Users/kimd/Sites/grovegraphix/directcableresource.com/xhtmlcss/images/homepage_flash/home_flash_3.swf
[SWF] Users:kimd:Sites:grovegraphix:directcableresource.com:xhtmlcss:images:homepage_flash:home_flash_3.swf - 140300 bytes after decompression
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at home_flash_3_fla::MainTimeline/frame1()[home_flash_3_fla.MainTimeline::frame1:9]
This is the AS3 code that I have:
//Go to the grovegraphix.com website
fiberjumpercables_btn.addEventListener(MouseEvent.CLICK,gotoGrove);
function gotoGrove(evtObj:Event):void{
var theSite:URLRequest = new URLRequest(”http://www.grovegraphix.com/directcableresource.com/xhtmlcss/landing_fiber_jumper_cables.html”);
navigateToURL(theSite,”_self”);
}
audiovideocables_btn.addEventListener(MouseEvent.MOUSE_DOWN, myHandler);
function myHandler(event:MouseEvent):void {
navigateToURL(new URLRequest(”http://www.grovegraphix.com/directcableresource.com/xhtmlcss/landing_audio_video.html”), “_self”);
}
The fiberjumpercables_btn is on frame 1.
The audiovideocables_btn is on frame 16.
I have 2 more buttons which I haven’t written code for yet.
fiberaccessories_btn on frame 34 and outletplug_btn on frame 61.
Sorry, I’m new to Action Script 3.0.
Thanks in advance for your help.
Kim
Kim - graphic designer and web designer
February 10th, 2010 at 12:10 am
Never mind, I got it figured out. Yah!! Here is the link.
http://www.grovegraphix.com/directcableresource.com/xhtmlcss/index.html
Click on each of the products and it will go to a landing page for that product.
Kim
Sean - CF Web Programmer
February 12th, 2010 at 12:48 pm
I have a CFForm with format=”flash” and I have a cfinput type=”button” within that CFForm. I am trying to place the event handler onClick into the button where, once clicked, a new window will appear. I have tried placing JS for the onClick and I have also tried AS. I can’t seem to get either to work. Can anyone help?
Thanks,
Sean