• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Error: 1021: Duplicate function definition

New Here ,
Apr 13, 2009 Apr 13, 2009

Copy link to clipboard

Copied

I am new to Flash, and I have downloaded the trial version of Flash CS4.  I have always been able to quickly learn and execute new programs, and after watching a bunch of great videos from this site and some tutorials, I was able to create nice Flash animations within hours.  I thought that this was amazing... until I tried to make a few functions in AS3 to add hyperlinks.

I have spent several days now trying to accomplish what should be a simple procedure... inserting hyperlinks for the buttons I created.  I was extremely frustrated with this to the point that I was (and still may) not going to purchase the entire Production Premium suite I was planning on this week.  I am still thinking about it after all my headaches.  I just can't believe that Adobe would make it so difficult for non-coders to make simple Flash animations that link to the websites that the animations promote.  Very disapointing, as links to websites is one of the most widely used advantages of Flash.  I understand that AS3 gives coders more powerful workflows, but non-coders who want to make simple Flash ads that link to websites cannot make it work very easily at all.  Adobe may find that the average person may steer away from the new Flash CS4 due to this extremely difficult aspect for what should be simple functions.

I kept working for another complete day on my project to get it to work trying everything I could think of, and going through many searches, and I was finally successful with one of the 9 buttons for 9 URL's in my project with the code below (substituted  actual site name for "mysite").

mysitebtn.addEventListener(MouseEvent.CLICK, onMouseClick);
function onMouseClick(e:MouseEvent):void
{
var request:URLRequest = new URLRequest('http://www.mysite.com');
navigateToURL(request,'_blank');
}

The problem I have now, I cannot get the other 8 buttons to work to target 8 other URL's.  I get this error: 1021: Duplicate function definition. function onMouseClick(e:MouseEvent):void

What can I do to correct this and allow other URL's to work in the animation with the 8 other buttons?  Any help with this would be greatly appreciated.

I have been using the CS2 Production Premium for a couple of years now, and I have really grown fond of the Adobe software.  I think it is the best package available for producers like myself.  I hope Adobe will look into helping out the non-coders for Flash CS4 or later versions in the future.  Why cut out an entire group of people from Flash?

Thank you in adance for any help.

Aaron

TOPICS
ActionScript

Views

7.3K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Apr 14, 2009 Apr 14, 2009

I'm not a good resource for acquiring learning resources.  Most of what I've learned is the result of the battlefield...  Chances are I started out much like you are doing now... learning code evolved over time.  But here's a brief lesson that might help (?)

Let's say you create a button symbol.  Since it is a button, it is already a self animating object that will react to mouse interactions, but only visually at this stage.  The first thing you need to do to make it useful code-wise is to assig

...

Votes

Translate

Translate
Guru ,
Apr 13, 2009 Apr 13, 2009

Copy link to clipboard

Copied

The issue here is that you are writing more than one onMouseClick listener function

Here's how I would recommend doing it, keep all the addEventListener calls for each button, but just use one onMouseClick function, doing something like this:

function onMouseClick(e:MouseEvent):void
{

var clickedButton:Button = Button(e.target);

var request:URLRequest;

switch (clickedButton){

case mysitebtn:

request = new URLRequest('http://www.mysite.com');
navigateToURL(request,'_blank');

break

case myotherbtn:

request= new URLRequest('http://www.myothersite.com');
navigateToURL(request,'_blank');

break;

default:

trace('you need to code something for '+clickedButton.name);

break

}



}

this is pretty rough, but something like the above should be what you're after

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 13, 2009 Apr 13, 2009

Copy link to clipboard

Copied

Thanks for posting a reply Greg.  I tried your code, but got three errors (probably because I didn't understand it).  Here is your code I tried with the exact spacing and the errors I received.

Code I tried:

function onMouseClick(e:MouseEvent):void
{

var clickedButton:Button = Button(e.target);

var request:URLRequest;

switch (clickedButton){

case mysitebtn:

request = new URLRequest('http://www.mysite.com');
navigateToURL(request,'_blank');


break

case myothersitebtn:

request= new URLRequest('http://www.myothersite.com');
navigateToURL(request,'_blank');


break;

default:


trace('you need to code something for '+clickedButton.name);

break



}

}

The three errors I received:

1046: Type was not found or was not a compile-time constant: Button.

1180: Call to a possibly undefined method Button.

Warning: 1060: Migration issue: The method Button is no longer supported.  The Button class has been renamed SimpleButton..

Thank you in advance for any help.

Aaron

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Apr 13, 2009 Apr 13, 2009

Copy link to clipboard

Copied

There is a Button class, but its a component. I guess you might be using SimpleButton if you didn't use the component Button,

try changing:

var clickedButton:Button = Button(e.target);

to:

var clickedButton:SimpleButton = SimpleButton(e.target);

let me know if that helps

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 14, 2009 Apr 14, 2009

Copy link to clipboard

Copied

If you don't understand the solution offered, you should consider a solution that you might better understand.  Your main problem is that you created different functions using the same function name for each, which cannot work.

Try creating a separate function for each button, with a unique function name for each.  Then have each event listener of each button call the unique function it is supposed to call.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 14, 2009 Apr 14, 2009

Copy link to clipboard

Copied

Hi guys, thank you for spending the time to reply to my question.  What you are telling me makes sense, however, since I have no idea about writing code, I don't know how to write a seperate function with a unique name and add an event listener for each unique function name.

I am a video producer/editor, and I can't write code.  This is why after just hours in a new program to me, Flash, I found it easy to create Flash animations, but can't make them function.  I am trying to learn, but find writing code difficult, especially AS3.

If you or someone else could please give me an example of how to write the code I need, I would really appreciate it.  I am into day 4 now, and I still can't make my Flash animation function with hyperlinks.  I realize this may be childs play for many of you, but since I am not someone who can write code, and Flash CS4 doesn't allow a simple hyperlink insert, it would be very helpful.

If someone knows a document I could read or video I could watch to learn how to write the code I need, I am willing to learn and read up.  I haven't found this information as of yet.  I will keep searching.

Thank you in advance,

Aaron

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 14, 2009 Apr 14, 2009

Copy link to clipboard

Copied

I'm not a good resource for acquiring learning resources.  Most of what I've learned is the result of the battlefield...  Chances are I started out much like you are doing now... learning code evolved over time.  But here's a brief lesson that might help (?)

Let's say you create a button symbol.  Since it is a button, it is already a self animating object that will react to mouse interactions, but only visually at this stage.  The first thing you need to do to make it useful code-wise is to assign it a unique instance name.  So you drag a copy of it out to the stage from the library, and while it's still selected, you enter that unique instance name for it in the Properties panel... let's say you name it "btn1"

In AS3, to make a button work with code, you need to add an event listener for it.  You might need to add a few (for different events, like rollover, rollout, clicking it, but for now we'll just say you want to be able to click it to get a web page to open.  In the timeline that holds that button, in a separate actions layer that you create, in a frame numbered the same as where that button exists, you would add the event listener:

btn1.addEventListener(MouseEvent.CLICK, btn1Click);

That line of code contains the following:

buttonInstanceName . displayObjectMethod (eventClass . eventType, eventHandlerFunction);

The name of the unique function for processing the clicking of that button was already defined at the end of the event listener assignment, so now you just have to write that function out:

function btn1Click(evt:MouseEvent):void {

   var url:String = "http://www.awebsite.com/awebpage.html";

   var req:URLRequest = new URLRequest(url);

   navigateToURL(req);

}

Here's some of what's involved there:

evt:MouseEvent - the event listeners throws an argument automatically which the function must be set up to receive.  In this case I have given that argument a variable name of evt, though I could have chosen anything.

:void - this defines the class of the value that the function will return.  In this case, the function does not return anything, so "void" is used.  If it did return a value, you would see a line containing "return xyz"; in the function (where xyz is not literal, it simply represents the variable or value being returned)

The normal code to open a web page contains three distinct elements, the url, the request, and the command to get the page, so I have shown them as three separate lines.

In AS3, in strict mode, it is necessary to identify the types/classes of the variables being created, which is why you see :String, :MouseEvent, etc... showing up everywhere.

I know that's probably clear as mud in the explanation, but hopefully it will shed some light on what you're working with.

Now, to create another button with a unique function for it, you could just drag another copy of it from the library, give it a unique name, say btn2, copy/paste the code from btn1 and replace "btn1" with "btn2" in that copied code, adding a new url for the page that button will open.

Someone may come along with some helpful info regarding books.  While I've never utilized it myself, I've seen www.Lynda.com recommneded here quite often as a good place to learn coding.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 14, 2009 Apr 14, 2009

Copy link to clipboard

Copied

Ned, thank you very much!  You were a huge help.

I was pleased that upon reading your last post, that almost everything you described I had followed exactly in building my animation.  The problem was with the code duplication.  Obviously, I knew the duplication was a problem, I just didn't know how to resolve it, until your last post.

I didn't know the code could be written that way, or more to the point, had to be written that way, (or that other way recommended here).  I just didn't understand the other way, but I understand your post perfectly, no mud at all.  It was really frustrating for a few days, but I finally have my animation working complete with functionality.  I'm sure it will be a long process to learn the different aspects of AS3, and I'm thankful for this forum and all the Adobe resources, and your post.

Thanks again!

Aaron

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 14, 2009 Apr 14, 2009

Copy link to clipboard

Copied

You're welcome.  One last thing to try to tie things together here.  As Greg mentioned his approach involves using one function for all the buttons to do the same thing that the approach I outlined does using individual functions for each button.  And Greg described pretty well what the inner working of that function involve.  The only thing that probably needs a little light to shine on it would be the role that the argument to the function plays, which in my example was evt:MouseEvent...

That variable ("evt", or whatever you decide to call it) can be used to identify the source of the event, which is the key to being able to create the one function for all.  The event listener passes that argument to the handler function.  That variable holds various pieces of information, one of which is the event 'target', meaning the object that was the target of the event, which is the button that is clicked.  Utilizing 'evt.target' essentially targets the button object, at which point you can find out its name... evt.target.name

That's about it... stay hooked

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Apr 14, 2009 Apr 14, 2009

Copy link to clipboard

Copied

I've attached a CS3 fla (or tried to, I see others have had problems...hopefully it will work!) showing the two different approaches outlined by Ned and I above, with just plain buttons.

If you consider this as two problems:

1. Running some (any) code when a button clicks


2. Navigating to a new url

then the fla shows you (based on the input from both Ned and I above) the two ways to get the above working. You can uncomment/comment out the alternate sections of the code (or delete the part you don't want) to get them working.

See if that helps more. Beyond that I can also recommend the O'Reilly 'Actionscript 3.0 Quick Reference Guide' as a useful reference for doing a lot of different 'bits and pieces' in actionscript as well as providing a great overview of different areas of actionscript development.

EDIT : I changed the name of the file to .fla.swf so it would permit attachment. You should remove the last .swf to leave it as a fla file again.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 14, 2009 Apr 14, 2009

Copy link to clipboard

Copied

Hi Greg, I would like to thank you very much as well.  I didn't understand your method to the code, but I will download your attachment, delete the .swf extention and dive into it to learn.  I'm not sure why there is two ways to provide code foe the same funtion, but I'm sure there is a reason.  It can't hurt to learn both methods of code.  Thanks for the book reference as well.  I will definitley look into it, as I'm sure it will be useful for a newbie to Flash and AS3.

I couldn't download your file for some reason?

Thanks again guys, you were both a big help!

Aaron

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Apr 14, 2009 Apr 14, 2009

Copy link to clipboard

Copied

You're welcome. It seems the attachment didn't take on my earlier post. It looks like I can't re-edit that message, I'll try again to attach it in this one, but if for some reason it doesn't work, then until I figure out how the attachments work correctly in this new forum system you can request it from me via greg[dot]dove[at]gmail.com

There are often several ways to accomplish the same thing in code, which is great because it lets people have preferences. Some people advocate one or another, for various reasons.

I would choose the way I did it because it becomes easier to maintain if you come back to make changes in 6 months time, but its also a matter of personal preference and Ned was right to point out the other option which could be considered more straightforward in the sense that switch/case seems a little daunting if you're seeing it for the first time or have never used it before.

The switch/case approach lets you channel all the button listeners through a single function.... and then if you ever need to add new buttons it's minimal changes to do that, and always in the same place in your code (the single listener function). Switch/case clauses are just an easier way of writing things like

if (clickedButton==btn1) { do something}

else if (clickedButton==btn2) {do something else};

else if (clickedButton==btn3) {do something completely differernt};

else {default action here }

But the above is valid code too (not the parts that say 'do something' etc)!

Basically switch/case works just like that above series of if/else statements, its just a little more readable - once you're familiar with it in code.

switch (clickedButton){

case btn1:

  do something

break;

case btn2:

  do something else

break;

case btn3:

  do something completely differernt

break;

default;

  default action here

break;

}

It may be difficult to figure that out if you've never used it before, but you should be able to see a pattern emerge from the code inside the function, where you just add a

case btnName:

    [custome code for this button here]

break;

for each button inside the switch part of the code.

I hope the above sheds a bit more light on the other approach... but if it doesn't, don't worry....the key thing is that you should do it the way that feels comfortable for you based on the amount of coding you expect to do and that gets you the results you need. I'm pretty sure we all started off the same way with coding even if that is our focus more than the design side of things.  I know I did. Its easy to forget that though....which is why it was great that Ned didn't.

I've reattached this file....get in touch if its not working and you want to take a look at it, I'll email it to you.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 15, 2009 Apr 15, 2009

Copy link to clipboard

Copied

LATEST

Again, thank you both very much.  You both helped me keep my sanity!

I am going to make it a point to learn Greg's method as well, as I plan to re-use my work and change the graphics and links in the future (can't get your attachment Greg, will send you an e-mail).

It would be nice to have a button to automatically insert hyperlinks etc. in a project in the program Flash CS4 for newbies like me, but I am willing to learn code, a neccessary evil in Flash CS4 LOL.

I am really thankful for the Adobe support forums and guys like you to help out the newbie like me.  Because of this, I have decided to go ahead and upgrade from CS2 Production Premium to CS4.. I just have to decide between Production Premium or Master Collection.

Thanks again guys, your time was very appreciated.

I'm sure you will see me again in this forum asking dumb AS3 and Flash newbie questions in the future.

All the best,

Aaron

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines