Skip to main content
Participating Frequently
April 23, 2009
Answered

'duplicate function definition' error

  • April 23, 2009
  • 2 replies
  • 4431 views

How should I change the code below in order to avoid getting an error?

It is giving me a 'duplicate function definition' error. Obviously I am not learning fast enough and made some mistake.

thumpy.addEventListerner(MouseEvent.CLICK, onClick);

function onClick(evt:MouseEvent):void {

   var url:String = "http://www.thumpersf.com";

   var req:URLRequest = new URLRequest(url);

   navigateToURL(req, "_blank");

}

This topic has been closed for replies.
Correct answer Ned Murphy

You are correct about the two lines being necessary.  One involves the assignment of the event listener with identification of the handler function in it:

humpy.addEventListener(MouseEvent.CLICK, onClick);

and the other is the actual event handler function

function onClick(....

The duplication error will arise when you have two functions sharing the same name...

function onClick(....

function onClick(....

Unfortunately I can't see the history of the posting while writing this, so I don't know if it's possible that some other function has been duplicated, rather than the one that is being focused on so far.

To answer your original question, and to rule in/out whether or not that code is the problem, change the code as follows:

thumpy.addEventListener(MouseEvent.CLICK, clickThumpy);

function clickThumpy(evt:MouseEvent):void {

   var url:String = "http://www.thumpersf.com";

   var req:URLRequest = new URLRequest(url);

   navigateToURL(req, "_blank");

}

(don't sweat shortcutting the code... if you're new to AS3 it's better to learn it realizing that there are three distinct parts to linking a URL as you have displayed... you can drive with one hand after you get some experience using two)

2 replies

funkysoul
Inspiring
April 23, 2009

I don't think that the typo would cause a duplicate function error, check your functions for duplicate names (onClick) is a common one

below a shorter version of the onClick function btw.

thumpy.addEventListener(MouseEvent.CLICK, onClick);

function onClick(evt:MouseEvent):void {

   navigateToURL(new URLRequest("http://www.thumpersf.com"), "_blank");

}

npqsterAuthor
Participating Frequently
April 23, 2009

I don't understand how to not 'duplicate' the onClick function

I thought that these two lines were necessary in order for the code to work:

thumpy.addEventListerner(MouseEvent.CLICK, onClick);

function onClick(evt:MouseEvent):void {

funkysoul
Inspiring
April 23, 2009

well it's pretty logic that you can't have 2 functions with the exact same name, how does flash know which function to call if you have 2 with the same name?

Let's assume you have 4 buttons, you setup their listeners for the click event you could write this function:

CODE BELOW:

button1.addEventListener(MouseEvent.CLICK, onClick);

button2.addEventListener(MouseEvent.CLICK, onClick);

button3.addEventListener(MouseEvent.CLICK, onClick);

button4.addEventListener(MouseEvent.CLICK, onClick);

function onClick(e:MouseEvent):void

{

switch(e.target.name)

{

case "button1":

//whatever you want to do

break;

case "button2":

//whatever you want to do

break;

case "button3":

//whatever you want to do

break;

case "button4":

//whatever you want to do

break;

}

}

with this you can create one single function that listens to all the button click events, you just need to be sure that you give your buttons a name.

Craig Grummitt
Inspiring
April 23, 2009

Try spelling 'addEventListerner' like this:

addEventListener

and see if that fixes the problem.

(although i would have thought that would give the error: TypeError: Error #1006: addEventListerner is not a function.)