2012-03-29 23 views
0

我在獲取語法時遇到了一些問題。刪除事件監聽器並將影片剪輯移動到位置

我有一個影片剪輯,它在觸摸其他影片剪輯時向數組添加聲音。 我有一個停止按鈕比我想刪除酒吧的事件監聽器,併發回到原來的位置。

我的代碼是:

//event listener for the start button 
playy.addEventListener(MouseEvent.CLICK, mouseClick2); 

function mouseClick2(event:MouseEvent):void 
{ 
    bar.addEventListener(Event.ENTER_FRAME, onEnter); 
} 


//Add event listener for the stop button 
stopp.addEventListener(MouseEvent.CLICK, mouseClick3); 


//when clicked remove listener send back to position 
function mouseClick3(event:MouseEvent):void 
{ 
    bar.removeEventListener(MouseEvent.CLICK, mouseClick3); 

    function mouseClick3(evt:Event):void 
    { 
     if(bar.x > 780) 
     { 
      bar.x = 215; 
     } 
    } 
} 


function onEnter(evt:Event):void 
{ 
    bar.x += 1; 

    if(bar.x > 780) 
    { 
     bar.x = 215; 
    } 

    for(var i:int=0; i<blocks.length;i++) 
    { 
     if (bar.hitTestObject(blocks[i])) 
     { 
      blocks[i].start(); 
     } 
     else 
     { 
      blocks[i].stopSound(); 
     } 
    } 
} 
+3

您遇到的一個問題是'function mouseClick3()'嵌套在另一個'function mouseClick3()'函數中。 – Marty 2012-03-29 22:39:17

回答

0

我猜,你的函數應該是這樣的:

//when clicked remove listener send back to position 
function mouseClick3(event:MouseEvent):void 
{ 
    bar.removeEventListener(MouseEvent.CLICK, mouseClick3); 

    if(bar.x > 780) 
    { 
     bar.x = 215; 
    } 
} 

的問題是,你在裏面有兩個「mouseClick3」功能。內部mouseClick3實際上並不執行,而removeEventListener將以永不執行的mouseClick3爲目標,因爲它是函數中的局部變量。如果您刪除內部mouseClick3,代碼將執行並且您的偵聽器將針對正確的函數。