事件监听器调用错误的函数

时间:2014-03-29 16:09:51

标签: actionscript audio

我在动作脚本3.0代码中需要帮助。我开发了一款射击游戏。问题是我有一个按钮sound_btn,用户需要点击播放声音。该按钮具有功能SoundButton,可以播放用户需要拍摄的字母字符(舞台上的5个不同按钮"字母1,2,3 ......等等#34;)。如果用户拍摄正确的答案图像将出现,他/她将得分。当用户向右拍摄按钮时,它将使用正确的_answer功能,如果错误则会转到错误的_answer功能。但遗憾的是射击目标无论是对还是错,它都转到right_answer函数我找不到错误。我把跟踪知道了i的值,似乎我生成了两个相同的值,我觉得这个函数运行了两次.i真的不知道答案请求帮助我。

    sound_btn.addEventListener(MouseEvent.CLICK, SoundButton);
    function SoundButton(evt:MouseEvent): void
    { //to hide the right marks
    right.visible=false;
 right2.visible=false;
 right3.visible=false;
 right4.visible=false;
 right5.visible=false;
 trace(i);

     if (i==0)
     { 

     myChannel = mysound.play();

    letter1.addEventListener(MouseEvent.CLICK, Right_Answer);
letter2.addEventListener(MouseEvent.CLICK, Wrong_Answer);
letter4.addEventListener(MouseEvent.CLICK, Wrong_Answer);
letter5.addEventListener(MouseEvent.CLICK, Wrong_Answer);
letter3.addEventListener(MouseEvent.CLICK, Wrong_Answer);


}

else if (i==1) 
     {

     myChannel1 = mysound1.play();

     letter1.addEventListener(MouseEvent.CLICK, Wrong_Answer);
letter2.addEventListener(MouseEvent.CLICK, Right_Answer);
letter4.addEventListener(MouseEvent.CLICK, Wrong_Answer);
letter5.addEventListener(MouseEvent.CLICK, Wrong_Answer);
letter3.addEventListener(MouseEvent.CLICK, Wrong_Answer);

}

else if (i==2)
{

    myChannel2 = mysound2.play();

    letter1.addEventListener(MouseEvent.CLICK, Wrong_Answer);
letter2.addEventListener(MouseEvent.CLICK, Wrong_Answer);
letter4.addEventListener(MouseEvent.CLICK, Right_Answer);
letter5.addEventListener(MouseEvent.CLICK, Wrong_Answer);
letter3.addEventListener(MouseEvent.CLICK, Wrong_Answer);

}

else if (i==3)
{

    myChannel3 = mysound3.play();

    letter1.addEventListener(MouseEvent.CLICK, Wrong_Answer);
letter2.addEventListener(MouseEvent.CLICK, Wrong_Answer);
letter4.addEventListener(MouseEvent.CLICK, Wrong_Answer);
letter5.addEventListener(MouseEvent.CLICK, Wrong_Answer);
letter3.addEventListener(MouseEvent.CLICK, Right_Answer);

}

else if(i==4)
{

    myChannel4 = mysound4.play();

    letter1.addEventListener(MouseEvent.CLICK, Wrong_Answer);
letter2.addEventListener(MouseEvent.CLICK, Wrong_Answer);
letter4.addEventListener(MouseEvent.CLICK, Wrong_Answer);
letter5.addEventListener(MouseEvent.CLICK, Right_Answer);
letter3.addEventListener(MouseEvent.CLICK, Wrong_Answer);


} 
    }
    function Wrong_Answer(event:MouseEvent)
    {
if (i==0){
 right.visible=true;
 i++;
 }
 else if (i==1){
     right2.visible=true;
     i++;
     }
 else if (i==2){
     right5.visible=true;
     i++;
     }
 else if (i==3){
     right3.visible=true;
     i++;
     }
 else if (i==4)
 {
     right4.visible=true;
     i++;

 }

 // move to next frame when the game is finish

     if (i>4)
     {
      nextFrame();
      }

  return i;

  //remove the event 
  letter1.removeEventListener(MouseEvent.CLICK, Wrong_Answer);
letter2.removeEventListener(MouseEvent.CLICK, Wrong_Answer);
letter3.removeEventListener(MouseEvent.CLICK, Wrong_Answer);
letter4.removeEventListener(MouseEvent.CLICK, Wrong_Answer);
letter5.removeEventListener(MouseEvent.CLICK, Wrong_Answer);



    }
    //show the right answer

    function Right_Answer(event:MouseEvent)
    {

 //for the scoring 
 score=score+1;
 score_show.text=(score).toString();
 i++;

 if (i>4)
     {
      nextFrame();
     }


 letter1.removeEventListener(MouseEvent.CLICK, Right_Answer);
 letter2.removeEventListener(MouseEvent.CLICK, Right_Answer);
 letter3.removeEventListener(MouseEvent.CLICK, Right_Answer);
 letter4.removeEventListener(MouseEvent.CLICK, Right_Answer);
 letter5.removeEventListener(MouseEvent.CLICK, Right_Answer);
 return i; 
     }

1 个答案:

答案 0 :(得分:0)

问题是您没有正确删除事件侦听器。你不能像这样添加这些事件

letter1.addEventListener(MouseEvent.CLICK, Wrong_Answer);
letter2.addEventListener(MouseEvent.CLICK, Wrong_Answer);
letter4.addEventListener(MouseEvent.CLICK, Wrong_Answer);
letter5.addEventListener(MouseEvent.CLICK, Right_Answer);
letter3.addEventListener(MouseEvent.CLICK, Wrong_Answer);

然后将它们删除:

letter1.removeEventListener(MouseEvent.CLICK, Wrong_Answer);
letter2.removeEventListener(MouseEvent.CLICK, Wrong_Answer);
letter3.removeEventListener(MouseEvent.CLICK, Wrong_Answer);
letter4.removeEventListener(MouseEvent.CLICK, Wrong_Answer);
letter5.removeEventListener(MouseEvent.CLICK, Wrong_Answer);

您现在已删除除以下所有事件侦听器:

letter5.addEventListener(MouseEvent.CLICK, Right_Answer);

如果你移动到下一帧,你将留下一个事件监听器,它会在下一帧触发错误的结果。随着时间的推移,你会积累越来越多的错误"你的字母和按钮上的结果和听众。

我建议你删除你添加的事件监听器。因此,您可以避免在每个字母上重复或多听众。