…the site you can never remember how to type…
So there have been a number of questions on my kinda recent post about making a button in Flash CS3 using ActionScript 3.0. I will do the next couple posts on how to do the things in the questions (and actually check em out in Flash, and make an FLA to download).
So if you haven’t already read or don’t know how to make a single button, head on over to the post linked above. Here, I’ll continue on to multiple buttons.
Say you have a couple buttons on a page, and you want one to go to one URL, and the second one to go to a different URL - you need to change your function names in your code so you don’t have multiples with the same name. So, you would need to do something like this:
thumbsdown_btn.addEventListener(MouseEvent.MOUSE_DOWN, thumbDownHandler); function thumbDownHandler(event:MouseEvent):void { navigateToURL(new URLRequest("http://msdn2.microsoft.com/en-us/silverlight/default.aspx")); } thumbsup_btn.addEventListener(MouseEvent.MOUSE_DOWN, thumbUpHandler); function thumbUpHandler(event:MouseEvent):void { navigateToURL(new URLRequest("http://www.adobe.com/devnet/flash/")); }
Note the thumbDownHandler and thumbupHandler in the code.
The source file: Mutliple buttons to URLs
So some people are instead targeting multiple frames. In that case, the code would look like this:
stop(); first_btn.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler1); function mouseDownHandler1(event:MouseEvent):void { gotoAndStop(5); } second_btn.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler2); function mouseDownHandler2(event:MouseEvent):void { gotoAndStop(10); }
The source file: Mutliple buttons to frames
In upcoming posts, I’ll cover targeting (say, targeting a parent timeline or scene or whatnot - some of the other questions in the original post).
This is Jen deHaan's latest blog in her neverending series of blogs. In the beginning there was deseloper. A designer developer. Now it's Flashthusiast, an enthusiast about Flash. Are you a Flashthusiast? Read on! This blog is about Adobe Flash, and will include tips, techniques and info on drawing, animation, and a bit on ActionScript 3.0. Jen is a QE on the Flash team at Adobe (San Francisco).
Chris
March 26th, 2008 at 9:04 am
Can you please tell me what to add to this code to make the page show up in the same window (of the browser?)
myBtn.addEventListener(MouseEvent.MOUSE_DOWN,mouseDownHandler);
function mouseDownHandler(event:MouseEvent):void
{
navigateToURL(newURLRequest(”http://www.flashthusiast.com/”));
}
Thomas
March 27th, 2008 at 11:16 am
Hello Flashthusiast,
Here I am again, in need for some help :-( I made a flashmovie in as3 and added three buttons that need to refer to different URL’s. Two of them work (home_btn and mixlawax_btn) but when I add the third button (moremusic_btn) I get an error report?
This is how I added my code in the actions pane:
mixlawax_btn.addEventListener(MouseEvent.MOUSE_DOWN, thumbDownHandler);
function thumbDownHandler(event:MouseEvent):void {
navigateToURL(new URLRequest(”http://www.mixlawax.com”));
}
home_btn.addEventListener(MouseEvent.MOUSE_DOWN, thumbUpHandler);
function thumbUpHandler(event:MouseEvent):void {
navigateToURL(new URLRequest(”http://www.morecult.com/”));
}
moremusic_btn.addEventListener(MouseEvent.MOUSE_DOWN, thumbUpHandler);
function thumbUpHandler(event:MouseEvent):void {
navigateToURL(new URLRequest(”http://www.mixlawax.com/radio/Radio2.php”));
What went wrong?? I can’t findout how to make all three work. And my geuss was if two buttons work the third would work too if I just copy past the code and adjust the btn name and URL to refer to…
Really hope you can help me out, I tried every possible thing to make it work, but I think I need a pro like you ;-) to set me back on track!
Thanks in advance.
Grtz
Thomas
Jen, a flashthusiast
March 27th, 2008 at 11:48 am
Hey Thomas!
In the moremusic_btn button, try renaming “thumbUpHandler” to a unique name, like thumbUpHandler2 or otherUpHandler. It looks like it’s conflicting with the “thumbUpHandler” function in the home_btn button.
Hope that works,
Jen.
Jen, a flashthusiast
March 27th, 2008 at 12:00 pm
Hey Chris,
You can set the target window right after the URL, pretty similar to AS2. So you can modify the navigateToURL linke as follows:
thumbsup_btn.addEventListener(MouseEvent.MOUSE_DOWN, thumbUpHandler);
function thumbUpHandler(event:MouseEvent):void {
navigateToURL(new URLRequest(”http://www.adobe.com/devnet/flash/”), “_self”);
}
Hope that works for ya,
Jen.
Anita
March 30th, 2008 at 12:35 am
I’ve also tried this, and have sort of the same kind of problem with one of the buttons not working. This is my code:
maria.addEventListener(MouseEvent.CLICK, gotomaria);
function gotomaria(event:MouseEvent):void {
navigateToURL(new URLRequest(”http://www.fef-aalborg.dk/testsite/sider/rapport/indien2008maria.html”), “_self”);
}
lisser.addEventListener(MouseEvent.CLICK, gotoingerlise);
function gotoingerlise(event:MouseEvent):void {
navigateToURL(new URLRequest(”http://www.fef-aalborg.dk/testsite/sider/rapport/indien2008ingerlise.html”), “_self”);
}
Both buttons, for some mysterious reason, go to the same page (the first URL specified), and I cannot find out what I’m doing wrong. I didn’t name anything the same, and I tried copy/paste to find out if I had overlooked an error in button two, but no. Help… :-)
peterd
March 30th, 2008 at 10:38 pm
Anita,
I’m not seeing the same issue locally. What about if you change the URLs to http://www.adobe.com and http://www.apple.com. Do they both still go to the same domain? What happens if you click the second button first? Do they always go to the first button’s URL?
maria.addEventListener(MouseEvent.CLICK, gotomaria);
function gotomaria(event:MouseEvent):void {
navigateToURL(new URLRequest(”http://www.adobe.com/”), “_self”);
}
lisser.addEventListener(MouseEvent.CLICK, gotoingerlise);
function gotoingerlise(event:MouseEvent):void {
navigateToURL(new URLRequest(”http://www.apple.com/”), “_self”);
}
Peter
Bav
April 1st, 2008 at 9:40 am
Hi, all
can anyone help me with this code?
I am trying to create 5 buttons that jump to different scenes
here is the code for one:
btn_1.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler3);
function mouseDownHandler3(event:MouseEvent):void {
gotoAndStop(1)
}
the problem is, I keep on getting this error:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at index_fla::MainTimeline/frame147()
what I am doing wrong here? any advice
please
jennicandance
April 2nd, 2008 at 10:24 am
I have had the same problem so if anyone can answer that it would help me out too…
Can someone post on how to create button rollovers using AS3? I’m having some trouble…
Thanks
Aisa chin
April 2nd, 2008 at 11:35 am
Hi there,
I work for a Telecommunications company in Calgary and came across your name.
Are you able to contract you to come out to calgary to give a presentation on Flash through Adobe or can it be through you.
And if you would be interested please do let me know. We would love to have you speak for us.
Thanks,
Aisa
Aisa Chin
April 2nd, 2008 at 11:40 am
Hi there,
I am emailing from the creative team at a telecommunications company in Calgary.
We would like to know if we are able to bring you in to talk about flash here in Calgary. Is that done through Adobe or can do you it as an on the side thing?
We would love to have you speak for us on flash.
It would be great to further discuss this if you could email me.
Thanks very much,
Aisa
heather
April 3rd, 2008 at 10:55 am
I was getting Error #1009 as well and figured out what was causing it. The keyframe in my “actions” layer where I applied by button action script occurred in frame 1, but the instance of my button didn’t appear until frame 5. Since the action occurred before the button showed up, that’s why I got the error. When I moved the keyframe of my actions layer to frame 5 (the same frame where my button first appears), the error went away.
For a better explanation, see this website:
http://curtismorley.com/2007/08/15/flash-cs3-flex-error-1009-cannot-access-a-property-or-method-of-a-null-object-reference/
Jen, a flashthusiast
April 5th, 2008 at 6:42 pm
Hi Aisa,
Unfortunately speaking and travel is generally up to Adobe. For the next while I’m pretty swamped with working on the software - but there might be someone available from Adobe (they do events and traveling in canada), or even from the local user group (I think it’s still running) who might be available to speak.
Best of luck,
Jen.
Bav
April 12th, 2008 at 2:55 am
right thanks, i have managed to sort out the errors. Now I have another problem (its just goes on) I have 5 buttons but they all seem to be jumping to scene 1, however I did managed to make the second button to jump on scene two. As soon as I add the third button it jumps to the first scene here is the code that I am applying to my scenes
stop()
btn_1.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler3);
function mouseDownHandler3(event:MouseEvent):void {
gotoAndStop(80);
}
btn_2.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler4);
function mouseDownHandler4(event:MouseEvent):void {
gotoAndStop(80);
}
btn_3.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler5);
function mouseDownHandler5(event:MouseEvent):void {
gotoAndStop(80);
and so on…..
any advice thanks ….
Dave
April 17th, 2008 at 7:16 am
Hi Jen - thanks for a great site - load of really helpful information.
I’ve created a flash menu bar which slides out from the right to reveal sub-menu options which I want the user to be able to click on to navigate further into the site. I’ve included the URL in my message to you - if you have time, would you please follow the URL, click on the “portfolio” button and offer some advice on how I can make the submenus active as they just take the property of the “portfolio” button at the moment. For the record, the “portfolio” button has been created as a button with up & over states while the submenus are purely text written with hyperlinks on the over state of the button.
Hope that all makes sense?
Xinivion
April 21st, 2008 at 5:43 pm
Hey i have looked and looked but have found nothing this here ismy last hope… i have a bunch of buttons on diferent keyframes and then i have buttons linking to each key frame.. i when there are buttons on the other keyframes it gives me a silly error message… is there a way to tell the comp. wut frame the button is on?
Monique
April 22nd, 2008 at 6:38 am
Im a little new in the CS3 version, so Im kinda confused…
I have a menu of 4 buttons inside a movie clip, how do I give the actions to the buttons there? should I do it in a different action layer or in each button?
Here is what I have so far, im not sure I have it right… I’ve written it like that for every button???? thnxs :)
home_btn.addEventListener(MouseEvent.MOUSE_DOWN,mouseDownHandler);
function mouseDownHandler(event:MouseEvent):void{
navigateToURL(newURLrequest(”http://roc-off.com”));
}
Monique
April 22nd, 2008 at 6:54 am
this is for the other buttons???
aboutus_btn.addEventListener(MouseEvent.MOUSE_DOWN,mouseDownHandler);
function mouseDownHandler2(event:MouseEvent):void{
navigateToURL(newURLrequest(”http://roc-off.com”));
}
Jen, a flashthusiast
April 22nd, 2008 at 11:15 am
Hi Xinivion — Make sure that the button code for the buttons at that keyframe is at the same frame. So if you have “buttonA” and “buttonB” at frame 10, make sure the code for that button is on a keyframe at frame 10 as well.
Jen, a flashthusiast
April 22nd, 2008 at 11:18 am
Hi Monique - Your first code snippet looks fine. The second one, make sure your mouseDownHandler’s match (one of them has a 2 - you should add the “2″ to the handler name in the first line.)
Monique
April 23rd, 2008 at 8:19 am
Thank you Jen, I tried doing what you said but, this is what I get with all the buttons… Im doing the action in the last frame of the timeline of each button inside the movie clip????
should I do this in a different layer for actions inside the timeline?
This is the errors i get
1120: Access of undefined property home_btn.
1180: Call to a possibly undefined method newURLrequest.
Tom
April 29th, 2008 at 10:38 am
Hi flashthusiast,
I developed a flash site for a photographer friend of mine and she is thrilled with it. She especially loves the opening, which resembles an unveiling. With that said, I am not happy with the funtionality of my buttons. If you move your mouse too quickly they stay in their rollover state. Is there a better approach to the functionality of these buttons. Right now, leaving the ‘hit’ area triggers the roll off state. Can you have a look? and maybe point me in the right direction? http://www.kristensomodywhalen.com
Thank you Jen, for your time.
Tom
tom2
May 2nd, 2008 at 7:46 am
hey there,
thx for your help on setting up multiple buttons…
here is MY question, one of my scenes i am jumping to from one of my buttons events has an embedded .flv playing. when i hit any other button for another scene, the sound from the movie continues to play.
how can i prevent this from happening? i’ve tried stop FLV, and stop all sounds etc, to no avail.
cheers,
t.
zall
May 4th, 2008 at 2:31 am
what is i have lots of buttons (15 for example). each of them should call the same function with different paramets. navigateToUrl for exmple. is there any other way to do this exept creating different handlers for each button.
with as 2 i did like that: i difined a url property for each button and used only one function. it must be the way to do the same in as3. i dont want to do 15 listeners and handlers
Dom
May 13th, 2008 at 6:37 am
Hi Everyone,
Really need some help with linking buttons to different scenes within my flash website that I am building. Also adding a stop function so that each scene stops before it moves onto the next. I used to be able to do this in AS1-2 but am totally confused by AS3. Can anyone help me? I would really appreciate it!
Thanks Dom.
Lisa
May 13th, 2008 at 11:16 am
Jen,
I’m new to Flash and trying to get buttons to work, need to use buttons to move back and forward by frame…your site has been great!
Here is my code from from frame 4, action layer (each frame in action layer has similiar code, 18 frames in all); my problem is listed below. I did my Next button code first…worked well, clicked from frame to frame just like I needed it too….then I started on the Back Button code…yikes
Thanks.
nextButton.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler3);
function mouseDownHandler3(event:MouseEvent):void {
gotoAndStop(4);
}
backButton.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler20);
function mouseDownHandler20(event:MouseEvent):void {
gotoAndStop(2);
}
The Problem: (example)
on frame 3, click back button, takes me to frame 2…then I click the Next button (still on frame 2) and it takes me to frame 4…I click the back button while on frame 4 and it takes me to frame 1!!!
Ali
May 17th, 2008 at 11:04 am
Dear Jen,
My comment is more about the AS in general. I’m a designer and not a CODER and I think Flash is not really a designer friendly application and the obvious reason for that is the AS language. May be time has come for Adobe to develop ways to address this issue instead of developing AS v4 :))) . I don’t know what they think at Adobe, but in my opinion designers are designers and coders are coders. Flash is not really for designers ;-). It is similar to think that in order to work with photoshop you need to learn VB :)))
Best Regards
Ali
Amy
May 19th, 2008 at 9:27 am
Just popping in to say thank you so much for the help! Like Ali (who posted right before me), I’m a designer, not a coder. So Flash gives me problems sometimes, but your blog has been a big help. Keep up the great work!
Kevin
May 24th, 2008 at 6:25 am
I get these errors:
1067: Implicit coercion of a value of type flash.display:SimpleButton to an unrelated type Function.
With this script
Scene 1:
Ashford : Frame 1
Ashford_btn.addEventListener(MouseEvent.MOUSE_DOWN, Ashford_btn);
function Ashford(event:MouseEvent):void {
navigateToURL(new URLRequest(”http://www.the-ragnarok.nl/images.html”));
}
Ascalon : Frame 1
Ascalon_btn.addEventListener(MouseEvent.MOUSE_DOWN, Ascalon_btn);
function Ascalon(event:MouseEvent):void {
navigateToURL(new URLRequest(”http://www.the-ragnarok.nl/home.html”));
}
Diane
June 1st, 2008 at 2:05 pm
Could someone help with code for BACK buttons using AS3? My NEXT buttons work just fine but when I click on the BACK buttons, they do go back to the specified frame and play that part of the animation and they do stop on the correct frame after the animation sequence is done playing, BUT when I click the NEXT button, rather than just playing the next animation sequence, it blows right through the next animation sequence and goes to and plays the following one. This is all on one timeline and one scene. Here is the code I’m using:
stop();
function onBackClick2(e:MouseEvent):void {
gotoAndPlay(”847″);
}
back_btn.addEventListener(MouseEvent.CLICK,onBackClick2);
function onNextClick3(e:MouseEvent):void {
gotoAndPlay(”1020″);
}
next_btn.addEventListener(MouseEvent.CLICK,onNextClick3);
THANK YOU!
Harry
June 2nd, 2008 at 12:03 am
Hi Jen
I am trying to make a survey type programme where the users clicks on either Yes or No on each frame. Then in the last frame it has the results of the survey. So the surveyer is able to see which questions the user answered yes and no to. I am having trouble with the last frame. Putting the answers into a dynamic txt box. Here is the code
stop();
btnYesS.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
function mouseDownHandler(event:MouseEvent):void {
txtSyringe.text = “Yes”
gotoAndStop(4);
}
btnNoS.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler2);
function mouseDownHandler2(event:MouseEvent):void {
txtSyringe.text = “No”
gotoAndStop(4);
}
Thanks!
Bibiana
June 15th, 2008 at 5:34 pm
Hi Jen!
If you could help me, I have 3 buttons. 2 of them dont have problem, but the middle one is giving me the error: 1120: Access of undefined property but_portfolio. why? thanks for any help!!
this is my code:
stop();
but_profile.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler1);
function mouseDownHandler1(event:MouseEvent):void {
gotoAndStop(73);
}
but_portfolio.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler2);
function mouseDownHandler2(event:MouseEvent):void {
gotoAndStop(73);
}
but_contact.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler3);
function mouseDownHandler3(event:MouseEvent):void {
gotoAndStop(73);
}
Chris
June 16th, 2008 at 4:46 am
Hi every body,
Can you help me with Actionscript 3.0 please?
I have a vertical scroll with 10 buttons (but_one, but_two, … etc.) and I have 10 FLV movies (mov_one, mov_two, … etc.) and I need to link each button to one of these movies so I can see one movie at a time (in the same position) every time I click on one of the buttons.
I have all the movies in one layer and I don“t know if it is a problem, cause when I tried to link each movie with one of these buttons with actionscript, I could see only the first movie (by clicking but_one) and when I click on any other button the first movie does not disapiared and or the first movie overlaps with the other movies when I click them.
Please, any body their can help me??
Thanks alot!
Amy Berg
June 17th, 2008 at 9:11 am
Hi, there –
I have a file with three scenes. They all reference buttons that navigate to content in “Scene2.” I can use these buttons to navigate to Scene2 from both Scene1 and Scene3, but once inside Scene2 they no longer work. However, if I play straight through Scene1 without navigating at all, the buttons in Scene2 will work. None of my code has changed and my file was working yesterday.
Any suggestions?
Thanks,
Amy