6 Replies - 4048 Views - Last Post: 30 April 2009 - 09:56 AM

#1 jpt420   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 38
  • Joined: 12-December 08

Linking buttons in flash using actionscript

Posted 15 April 2009 - 12:48 PM

I am trying to make multiple buttons with multiple links in flash using action script. I can make one button with one lik just fine but when I try to add buttons and links it doesnt work

here is my code:
home_btn.addEventListener(MouseEvent.CLICK,homeF);

function homeF(e:MouseEvent):void {
	var homeLink:URLRequest = new URLRequest("http://www.google.com");
	navigateToURL(homeLink, "_blank");
	
}
contact_btn.addEventListener(MouseEvent.CLICK,homeF);

function homeF(e:MouseEvent):void {
	var homeLink:URLRequest = new URLRequest("http://www.yahoo.com");
	navigateToURL(homeLink, "_blank");


and here is the error I get:
Description:   1021:Duplicate function definition.	 Source:   function homeF(e:MouseEvent):void {


any help?

Is This A Good Question/Topic? 0
  • +

Replies To: Linking buttons in flash using actionscript

#2 blake912   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 15-April 09

Re: Linking buttons in flash using actionscript

Posted 15 April 2009 - 03:31 PM

Okay, your issue is that you are telling the event listeners to go to the same function... and then pasting the same function multiple times.
to break it down for you:

contact_btn.addEventListener(MouseEvent.CLICK, homeF);

in the above:
contact_btn is the button that you want to listen for an event on
.addEventListener is telling the the program to listen for an event (at this point it doesn't know what type of event to listen for)
(MouseEvent.CLICK, homeF); MouseEvent.CLICK says to listen for a click on that button. then after the coma.... it says the function to execute when the button is clicked.


Your function needs to use (event:MouseEvent) instead of (e:MouseEvent) as well
So for this reason... you can have only one function per name. I made some adjustments to the structure of the code as well, such as grouping the event listeners. Look at the corrected code and let me know if you need any more help:

home_btn.addEventListener(MouseEvent.CLICK, homeClicked);
contact_btn.addEventListener(MouseEvent.CLICK, contactClicked);

function homeClicked(event:MouseEvent):void 
{
	var homeLink:URLRequest = new URLRequest("http://www.google.com");
	navigateToURL(homeLink, "_blank");
}

function contactClicked(event:MouseEvent):void 
{
	var homeLink:URLRequest = new URLRequest("http://www.yahoo.com");
	navigateToURL(homeLink, "_blank");
}


This post has been edited by blake912: 15 April 2009 - 04:37 PM

Was This Post Helpful? 0
  • +
  • -

#3 jpt420   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 38
  • Joined: 12-December 08

Re: Linking buttons in flash using actionscript

Posted 20 April 2009 - 10:16 AM

Thank you for the help but I am still having problems with the code it gives me this error when I use your code:
(Scene 1 layer 'layer 1', frame 1, line 1) (1120: Access of undefined property home_btn.) (home_btn.addEventListener(MouseEvent.CLICK,homeF);)

(Scene 1 layer 'layer 1', frame 1, line 8) (1120: Access of undefined property contact_btn.) (contact_btn.addEventListener(MouseEvent.CLICK,homeG);)


here is the code:
home_btn.addEventListener(MouseEvent.CLICK,homeF);

function homeF(e:MouseEvent):void {
	var homeLink:URLRequest = new URLRequest("http://www.google.com");
	navigateToURL(homeLink, "_blank");
	
}
contact_btn.addEventListener(MouseEvent.CLICK,homeG);

function homeG(e:MouseEvent):void {
	var homeLink:URLRequest = new URLRequest("http://www.yahoo.com");
	navigateToURL(homeLink, "_blank");
	
}

Was This Post Helpful? 0
  • +
  • -

#4 Dorito_125   User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 88
  • Joined: 13-December 08

Re: Linking buttons in flash using actionscript

Posted 23 April 2009 - 09:12 PM

View Postjpt420, on 20 Apr, 2009 - 09:16 AM, said:

Thank you for the help but I am still having problems with the code it gives me this error when I use your code:
(Scene 1 layer 'layer 1', frame 1, line 1) (1120: Access of undefined property home_btn.) (home_btn.addEventListener(MouseEvent.CLICK,homeF);)

(Scene 1 layer 'layer 1', frame 1, line 8) (1120: Access of undefined property contact_btn.) (contact_btn.addEventListener(MouseEvent.CLICK,homeG);)


here is the code:
home_btn.addEventListener(MouseEvent.CLICK,homeF);

function homeF(e:MouseEvent):void {
	var homeLink:URLRequest = new URLRequest("http://www.google.com");
	navigateToURL(homeLink, "_blank");
	
}
contact_btn.addEventListener(MouseEvent.CLICK,homeG);

function homeG(e:MouseEvent):void {
	var homeLink:URLRequest = new URLRequest("http://www.yahoo.com");
	navigateToURL(homeLink, "_blank");
	
}


Go to the stage and make sure your button has an instance name of "home_btn", and your contact button is "contact_btn"
Was This Post Helpful? 1
  • +
  • -

#5 blake912   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 15-April 09

Re: Linking buttons in flash using actionscript

Posted 27 April 2009 - 04:12 PM

instance names can be found in the properties box. If you don't have one select "window" from the top of the application. Then "properties"
Was This Post Helpful? 1
  • +
  • -

#6 jpt420   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 38
  • Joined: 12-December 08

Re: Linking buttons in flash using actionscript

Posted 29 April 2009 - 12:30 PM

Thank you, that fixed the problem
Was This Post Helpful? 0
  • +
  • -

#7 blake912   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 15-April 09

Re: Linking buttons in flash using actionscript

Posted 30 April 2009 - 09:56 AM

View Postjpt420, on 29 Apr, 2009 - 11:30 AM, said:

Thank you, that fixed the problem


Glad to help!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1