Skip navigation
boyonfire
Currently Being Moderated

1021: Duplicate function definition.

May 16, 2012 7:23 PM

Well, I am trying to generate a random number and send the users to that keyframe in flash but I keep getting the errors

Symbol 'WholeFile', Layer 'actions', Frame 3, Line 3 1151: A conflict exists with definition speed in namespace internal.

Symbol 'WholeFile', Layer 'actions', Frame 3, Line 4 1021: Duplicate function definition.

on each of the keyframes for the random number.

Here is the code:

On the main generator

  var high:Number = 13;

  var low:Number= 1;

 

  var district = (Match.floor(Math.random()*(1+high-low))+low).toString();

 

if(district == 1)

{

  gotoAndStop("district1");

}

else if(district == 2)

{

  gotoAndStop("district2");

}

  else if(district == 3)

{

  gotoAndStop("district3");

}

  else if(district == 4)

{

  gotoAndStop("district4");

}

else if(district == 5)

{

  gotoAndStop("district5");

}

else if(district == 6)

{

gotoAndStop("district6");

}

  else if(district == 7)

{

  gotoAndStop("district7");

}

  else if(district == 8)

{

  gotoAndStop("district8");

}

  else if(district == 9)

{

  gotoAndStop("district9");

}

  else if(district == 10)

{

  gotoAndStop("district10");

}

  else if(district == 11)

{

  gotoAndStop("district11");

}

else if(district == 12)

{

  gotoAndStop("district12");

}

else if (district == 13)

{

  gotoAndStop("capitol");

}

 

on the generated number frame

import fl.transitions.*;

import fl.transitions.easing.*;

var speed:Number = 3;

function comeToMe(event:MouseEvent){

   var obj:Object = event.target;

   var tweenX:Tween = new Tween(plyr, "x", None.easeNone, plyr.x, obj.x, speed, true);

   var tweenY:Tween = new Tween(plyr, "y", None.easeNone, plyr.y, obj.y, speed, true);

   addEventListener(Event.ENTER_FRAME, loopFunction);

   var obj_clip:MovieClip = obj as MovieClip;

   function loopFunction(event:Event):void{

      if(plyr.hitTestObject(obj_clip)) {

        status_txt.text = "Welcome to "+obj_clip.name+" have fun!";

        tweenX.stop();

        tweenY.stop();

        removeEventListener(Event.ENTER_FRAME, loopFunction);

      }

   }

}

bank.addEventListener(MouseEvent.CLICK, comeToMe);

home.addEventListener(MouseEvent.CLICK, comeToMe);

business.addEventListener(MouseEvent.CLICK, comeToMe);

 

please help and tell me what's wrong with my code!!

 
Replies
  • Currently Being Moderated
    May 17, 2012 12:17 AM   in reply to boyonfire

    var district = (Match.floor(Math.random()*(1+high-low))+low).toString();

     

    In this Math is misspelled as Match. Change it to Math.floor.

     
    |
    Mark as:
  • Currently Being Moderated
    May 17, 2012 9:13 PM   in reply to boyonfire

    I checked this code but didn't get any errors

     
    |
    Mark as:
  • Currently Being Moderated
    May 17, 2012 9:17 PM   in reply to boyonfire

    Your code is working fine for me. Once check speed variable is declared anywhere in your code. If it is then change speed variable name to some other name. If it is not then uncheck the option "Automatically declare stage instances" in Action Script 3.0 publish settings.

     

    Coming to duplicate function defintion error, Check for comeToMe function also. If there is any duplicate find somewhere in your code then change it. If it is not then try with different function names for addEventListeners.

     
    |
    Mark as:
  • Currently Being Moderated
    May 18, 2012 5:31 PM   in reply to boyonfire

    Do you happen to be repeating code in different frames?

     
    |
    Mark as:
  • Currently Being Moderated
    May 19, 2012 4:27 AM   in reply to boyonfire

    You can share functions and variables along a timeline if they do the same thing, meaning you do not need to rewrite them in different frames.  If they happen to do diferent things, then you need to use new names.  Similarly, you can share variables.  If you declare a function or variable in a frame, if you extend that layer along the timeline they can be used along the length of the timeline in which they extend.

     

    So in frame 1, if you declare a variable...

     

    var someVar:* = initialValue;   // * represents its Class type

     

    you can reuse that variable anywhere along the timeline, but you cannot redeclare (cannot use " var someVar:* " again) again for it anywhere else along that timeline.  The same with functions... in frame 1 you could have:

     

    function doSomething(argument:*):void {

         // do something

    }

     

    and in later frames you can call that same function using: doSomething(arg);  but you cannot create another function by the same name (cannot use "function doSomething(...)" again)

     
    |
    Mark as:
  • Currently Being Moderated
    May 19, 2012 6:11 AM   in reply to boyonfire

    One other thing worth mentioning is that when it comes to functions, you can often have one function that is written generically such that it makes use of arguments or external variables to process different things in a similar manner. 

     

    An example of this would be a function that processes different buttons being clicked.  If the processing is the same for each button but invoilves different instances of objects, you can use the event argument that gets passed to the function to determine which button was clicked and use that information to process relative to that button.

     

    function buttonClicked(evt:MouseEvent):void {

         // "evt.target" is the object that dispatched the event

         // "evt.currentTarget" is the object with the listener assigned to it

         // sometimes evt.target is the same as evt.currentTarget, sometimes not.

    }

     
    |
    Mark as:
  • Currently Being Moderated
    May 19, 2012 10:11 AM   in reply to boyonfire

    The 4th line of what you show doesn't make sense, and is where your problem is anyways.  When you call a function you do not include the class identifier ":MouseEvent"    But since that function appears to be called by buttons as an event handler function, you probably do not want to be calling it that way... try removing that line altogether or explain why you have it there.

     
    |
    Mark as:
  • Currently Being Moderated
    May 22, 2012 6:10 AM   in reply to Ned Murphy

    Hi Ned,

     

    I'm having the same issue as boyonfire, where I have a few buttons that spread thru different frames but I can't obviously having the same code repeating in different frames.

     

    I guess what he was trying to do was what you said before "and in later frames you can call that same function using: doSomething(arg);"

     

    So he just wrote "comeToMe(event:MouseEvent);" The name of his function plus what was inside the parentesis. That's how he interperted your "doSomething(arg);"

     

    But since this not work, we now know this is not the right way to do it...

     

    Can you please explain how we should be calling the function instead? And yes, the function is being called by a button.

     

    Cheers,

     
    |
    Mark as:
  • Currently Being Moderated
    May 22, 2012 6:23 AM   in reply to xlifes

    The problem with what was done... comeToMe(event:MouseEvent);  was as I already explained.  You do not include the class of the argument when you call a function with an argument. 

     

    If calling that function using that "event" argument was a valid approach (which it can be, but is also questionable since an event listener argument is normally passed without code specifying it), then the function call would have been...

     

         comeToMe(event); 

     

    the ":MouseEvent" does not get included in the function call.

     

    If the function is being called by an event listener, then you don't call it at all, you just specify it in the listener without any mention of the event argument...

     

        someBtn.addEventListener(MouseEvent.CLICK, comeToMe);

     

    as I mentioned already, the event argument is automatically passed the the event handler function, so you do not need to specify it in the listener.

     
    |
    Mark as:
  • Currently Being Moderated
    May 22, 2012 6:33 AM   in reply to Ned Murphy

    Thanks Ned, this solved the problem. It's quite simple now that you've pointed it out!

     

    We just need to write the  function in the first frame and then, whenever we want to call it again in other frames, just use the "someBtn.addEventListener(MouseEvent.CLICK, comeToMe);"

     

    Again, thanks for this. It was very helpfull!

     
    |
    Mark as:
  • Currently Being Moderated
    May 22, 2012 9:20 AM   in reply to xlifes

    You're welcome

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points