1021: Duplicate function definition in my action S

2019-08-12 17:04发布

问题:

This is a follow up to my previous question (Problems Scripting Multiple Buttons(nearly identical) in a single Action Script)

I am making an interactive flash Project...It has has 17 Separate scenes ...

  • Intro Scene
  • "Main_ Sequence"
  • 15 Individually title song pages ....

Where my first issue is the "main sequence" has 15 Buttons and I need to link them to the 15 separate scenes ...I am using the following code...

    TD_g.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
    s_g.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
    ats_g.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
    iyk_g.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
    hms_g.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
    tf_g.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
    hd_g.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
    ld_g.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
    ll_g.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
    ts_g.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
    ipsy_g.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
    ysm_g.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
    ihm_g.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
    iss_g.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
    tl_g.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
    function mouseDownHandler(event:MouseEvent):void {
   var nameOfButton:String=event.currentTarget.name;
   if (nameOfButton=="TD_g") {
    gotoAndStop(1, "Tweedlee_Dee");
} else if (nameOfButton=="s_g") {
    gotoAndStop(1, "Sincerely");
} else if (nameOfButton=="ats_g") {
    gotoAndStop(1, "Ain’_that_a_shame");
} else if (nameOfButton=="iyk_g") {
    gotoAndStop(1, "I_hear_you_knocking");
} else if (nameOfButton=="hms_g") {
    gotoAndStop(1, "Hearts_made_of_stone");
} else if (nameOfButton=="tf_g") {
    gotoAndStop(1, "Tutti_fruiti");
} else if (nameOfButton=="hd_g") {
    gotoAndStop(1, "Hound_Dog");
} else if (nameOfButton=="ld_g") {
    gotoAndStop(1, "Little_darlin");
} else if (nameOfButton=="ll_g") {
    gotoAndStop(1, "Louie_Louie");
} else if (nameOfButton=="ts_g") {
    gotoAndStop(1, "Twist_and_shout");
} else if (nameOfButton=="ipsy_g") {
    gotoAndStop(1, "I_put_a_spell_on_you");
} else if (nameOfButton=="ysm_g") {
    gotoAndStop(1, "You_shook_me");

} else if (nameOfButton=="ihm_g") {
    gotoAndStop(1, "I_can_hear_music");

} else if (nameOfButton=="iss_g") {
    gotoAndStop(1, "I_shot_the_sheriff");

} else if (nameOfButton=="tl_g") {
    gotoAndStop(1, "Tainted_love");
}
    }

when I run the sequence I get the following 15 errors All 1021: Duplicate function definition. Source : function mouseDownHandler(event:MouseEvent):void {

I tried changing the ..._g.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);to include a unique #next to each DownHandler ex) TD_g.addEventListener12(MouseEvent.MOUSE_DOWN, mouseDownHandler1);..I still get 15 errors...

Thanks !

P.S ...I am also looking to insert stop(); somewhere in that action script so that after the animation is played for this scene people have the opportunity to navigate and click the buttons instead of it "jumping" to the next scene!

回答1:

Personally I prefer using hash map/Dictionary to handle name/value pairs like these. So your code could be written as:

var buttonMap:Dictionary = new Dictionary();
buttonMap["TD_g"] = "Tweedlee_Dee";
buttonMap["s_g"] = "Sincerely";
buttonMap["ats_g"] = "Ain’_that_a_shame";
buttonMap["iyk_g"] = "I_hear_you_knocking";
buttonMap["hms_g"] = "Hearts_made_of_stone";
buttonMap["tf_g"] = "Tutti_fruiti";
buttonMap["hd_g"] = "Hound_Dog";
buttonMap["ld_g"] = "Little_darlin";
buttonMap["ll_g"] = "Louie_Louie";
buttonMap["ts_g"] = "Twist_and_shout";
buttonMap["ipsy_g"] = "I_put_a_spell_on_you";
buttonMap["ysm_g"] = "You_shook_me";
buttonMap["ihm_g"] = "I_can_hear_music";
buttonMap["iss_g"] = "I_shot_the_sheriff";
buttonMap["tl_g"] = "Tainted_love";

TD_g.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
s_g.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
ats_g.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
iyk_g.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
hms_g.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
tf_g.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
hd_g.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
ld_g.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
ll_g.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
ts_g.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
ipsy_g.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
ysm_g.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
ihm_g.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
iss_g.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
tl_g.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);

function mouseDownHandler(event:MouseEvent):void {
    gotoAndStop(1, buttonMap[event.currentTarget.name]);
}

Still that doesn't solve your problem. The error says you have function mouseDownHandler defined somewhere else in your code. Click on the lines with the errors in compiler errors and find the duplicate code. Comment it for now then figure out why it was copy/pasted in the first place and improve/fix that. HTH