2013-04-05 116 views
0

總之,這裏是希望我來完成:添加/兒童影片剪輯AS3刪除事件監聽器/

  1. 單擊電影剪輯,添加子
  2. 點擊子影片剪輯,播放再次響起
  3. 點擊孩子,停止發聲
  4. 點擊孩子第三次,除去孩子

可悲的是,我只得到了儘可能的步驟1.我已經找到了如何獲得如此當我點擊父級影片剪輯(我正在使用連接)時播放和播放,但是當我嘗試與孩子後一樣時,出現以下錯誤:

TypeError:Error#1010:Term is undefined並沒有任何屬性。 (我不再收到此錯誤)

場景1,圖層'操作',第1幀,第29行1120:訪問未定義屬性newBox。

 

    leftBox.addEventListener(MouseEvent.CLICK, addBox); 
    function addBox(event:MouseEvent):void 

    { 
    var newBox:right_box = new right_box(); 
    addChild(newBox); 
    newBox.x = 0; 
    newBox.y = 0; 
    newBox.width = leftBox.width; 
    newBox.height = leftBox.height /2; 

    } 
    newBox.addEventListener(MouseEvent.CLICK, playSound); 
    function playSound(event:Event) 
    { 
    var mySound:testSound = new testSound(); 
    mySound.play(); 

    } 

任何幫助將不勝感激。

謝謝!

(PS我是一個的n00b,所以請很好!)

+0

請將您認爲有問題的代碼片段添加到問題中。 – Ihsan 2013-04-05 20:19:02

+0

@Ihsan,我剛剛添加了片段。但問題肯定是playSound功能。當我評論它時,一切正常。 – user2247402 2013-04-05 21:07:14

回答

0

您嘗試添加一個事件偵聽newbox,其創建之前..如下嘗試:

// mySound should be availible in scope 
var mySound:testSound = new testSound(); 

// newBox also 
var newBox:right_box; 
// here is a channel for you 
var channel: SoundChannel; 

// ok this adds the first listener... 
leftBox.addEventListener(MouseEvent.CLICK, addBox); 


function addBox(event:MouseEvent):void { 
    newBox = new right_box(); 
    addChild(newBox); 
    newBox.x = 0; 
    newBox.y = 0; 
    newBox.width = leftBox.width; 
    newBox.height = leftBox.height /2; 
    // you should add listener here... 
    newBox.addEventListener(MouseEvent.CLICK, playSound); 
    // you have to avoid multiple newBoxes on each other and 
    // leaving the older ones under.. 
    // So stop listening to the newBox generating event: 
    leftBox.removeEventListener(MouseEvent.CLICK, addBox); 
} 

function playSound(event:Event){ 
    channel = mySound.play(); 
    // On next click you want sound to stop so 
    // First remove the old listener to avoid play over: 
    newBox.removeEventListener(MouseEvent.CLICK, playSound); 
    // and hook listener to stop method 
    newBox.addEventListener(MouseEvent.CLICK, stopSound); 
} 

function stopSound(event:Event){ 
    channel.stop(); 
    // On next click you want to remove newBox 
    // First remove the old listener to avoid play over: 
    newBox.removeEventListener(MouseEvent.CLICK, stopSound); 
    newBox.addEventListener(MouseEvent.CLICK, removeNewBox); 
} 

function removeNewBox(event:Event){ 
    // First remove the listener : 
    newBox.removeEventListener(MouseEvent.CLICK, removeNewBox); 
    removeChild(newBox); // now remove from display list 
    newBox = null; // make contents eligible for garbage collection 
} 
+0

嗯,現在我在'newBox.removeEventListener(MouseEvent.CLICK,playSound);'上得到了「1120:未定義屬性newBox的訪問」。 – user2247402 2013-04-05 21:23:34

+0

因爲你已經將它定義爲一個函數中的var,所以它不能在函數範圍外使用(符號newBox指向沒有任何東西)... – Ihsan 2013-04-05 21:25:20

+0

我在想這可能是我的問題的一部分!但是因爲我用MouseEvent來使用它,那麼什麼語法纔有意義? – user2247402 2013-04-05 21:28:29