Skip to main content
Participant
February 24, 2013
Answered

Error 1021: Duplicate function definition.

  • February 24, 2013
  • 1 reply
  • 1093 views

Hello i have little problem.. i want to make 2-5 different buttons but have the same action script.
The 1st one is perfect using the code. but when i make another button and duplicate the action script to link it.
problem occu Error 1021: Duplicate function definition.

here is my code:

-----

playPauseToggle_mc.addEventListener(MouseEvent.CLICK, fl_togglePlayPause);

function fl_togglePlayPause(evt:MouseEvent):void

{

   

    if(playPauseToggle_mc.currentLabel == "play")

    {

       

        playPauseToggle_mc.gotoAndStop("pause");

    }

    else if(playPauseToggle_mc.currentLabel == "pause")

    {

        playPauseToggle_mc.gotoAndStop("play");

    }

}

playPauseToggle2_mc.addEventListener(MouseEvent.CLICK, fl_togglePlayPause);

function fl_togglePlayPause(evt:MouseEvent):void

{

   

    if(playPauseToggle2_mc.currentLabel == "play")

    {

       

        playPauseToggle2_mc.gotoAndStop("pause");

    }

    else if(playPauseToggle2_mc.currentLabel == "pause")

    {

        playPauseToggle2_mc.gotoAndStop("play");

    }

}

----

Please help me with this one. Thank you in advance.

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

The primary problem with what you did is exactly what the error message is indicating.  You are defining two different functions with the exact same function name...   fl_togglePlayPause

If you were to rename the second function to be  fl_togglePlayPause2, then that would solve that problem.

But for the code you are showing you could just as easily have all of the buttons use the same single function if you just rewrite that function a little bit to be generic by using the event information...

function fl_togglePlayPause(evt:MouseEvent):void

{

   

    var toggleBtn:MovieClip = MovieClip(evt.currentTarget);

    if(toggleBtn.currentLabel == "play")

    {

       

        toggleBtn.gotoAndStop("pause");

    }

    else if(toggleBtn.currentLabel == "pause")

    {

        toggleBtn.gotoAndStop("play");

    }

}

With that approach you only need to have the one function defined and all of your buttons can share it.

1 reply

Ned Murphy
Ned MurphyCorrect answer
Legend
February 24, 2013

The primary problem with what you did is exactly what the error message is indicating.  You are defining two different functions with the exact same function name...   fl_togglePlayPause

If you were to rename the second function to be  fl_togglePlayPause2, then that would solve that problem.

But for the code you are showing you could just as easily have all of the buttons use the same single function if you just rewrite that function a little bit to be generic by using the event information...

function fl_togglePlayPause(evt:MouseEvent):void

{

   

    var toggleBtn:MovieClip = MovieClip(evt.currentTarget);

    if(toggleBtn.currentLabel == "play")

    {

       

        toggleBtn.gotoAndStop("pause");

    }

    else if(toggleBtn.currentLabel == "pause")

    {

        toggleBtn.gotoAndStop("play");

    }

}

With that approach you only need to have the one function defined and all of your buttons can share it.