2013-04-05 114 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