View Full Version : Buttons linking to URL Problems
neophyte-2
February 12th, 2008, 04:48 PM
Hi,
I'm having trouble linking my buttons to a URL. Keep getting and error message on my function line. Will someone help by looking at my code and teling me where I screwed up. I'm using AS 3.
thanks for the help- I know this is simple stuff and I appreciate your expertise.
import flash.events.MouseEvent;
var getMore:URLRequest = new URLRequest("http://www.nyc.gov/html/doh/html/condoms/condoms-press.shtml");
//--moreButn Get URL--\\
getMore.addEventListener(MouseEvent.CLICK,bClick);
function bClick(event:MouseEvent):void{
navigateToURL(getMore);
}
Escape-Artist
February 12th, 2008, 04:59 PM
Can you tell us what the error is please?
neophyte-2
February 12th, 2008, 05:26 PM
The error is:
1061: Call to a possibly undefined method addEventListener through a reference with static type flash.net:URLRequest.
Thanks
Escape-Artist
February 12th, 2008, 06:02 PM
Oh yeah didnt see that...
You're adding the click event on the URLRequest variable. You should add that to your button or movieclip.
neophyte-2
February 12th, 2008, 06:05 PM
Would you mind showing me what you mean?
neophyte-2
February 12th, 2008, 06:12 PM
Oh wait. I think the trouble is that I've named the var and the object (my button) both with the same name: getMore.
Do you think it woudl help if I changed one of thier names?
I'll give it a shot.
thanks for your help.
a
neophyte-2
February 12th, 2008, 06:15 PM
that worked.
thanks much!
Escape-Artist
February 12th, 2008, 06:21 PM
Sure:
So you have buttons on stage right, when you click on them, they link to a url.
Say your first button is a movieclip called mc_link1.
You add the event listener on that button:
mc_link1.addEventListener( MouseEvent.MOUSE_DOWN, bClick )
Now if you click on mc_link1 it will take you to the url in getMore.
The only thing with that is your function bClick has the getMore value hardcoded so everytime you will call bClick it will take you to the url in getMore.
What you could do is for each button have a different function with a different URLRequest in it, but that would be really dirty.
How I'd do it is use delegate.
Say you have 3 buttons: mc_link1, mc_link2 and mc_link3.
add the event listener with the delegate ( allows you to give arguments to the function you call ). For the delegate I use the one from the lowRa framework from Francis Bourre ( you can get it here: http://www.osflash.org/projects/lowra
You need to import the delegate class first, then add the listeners:
import com.bourre.commands.Delegate;
mc_link1.addEventListener( MouseEvent.MOUSE_DOWN, Delegate.create( bClick, "http://www.domain1.com" ) );
mc_link2.addEventListener( MouseEvent.MOUSE_DOWN, Delegate.create( bClick, "http://www.domain2.com" ) );
mc_link3.addEventListener( MouseEvent.MOUSE_DOWN, Delegate.create( bClick, "http://www.domain3.com" ) );
now all the buttons call the same function, and give it a different argument ( the url they point to ). So bClick now looks like that:
function bClick( url:String, e:*=null ):void{
navigateToURL( new URLRequest( url ) );
}
That's just a cleaner way to do it, but if you think it's too complicated just go for the normal way.
If you want to keep what you have, just make sure you add the event listener to your button not the URLRequest.
++
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.