0

简而言之,这是我想要完成的:

  1. 点击影片剪辑,添加孩子
  2. 单击子影片剪辑,播放声音
  3. 再次点击孩子,停止声音
  4. 第三次点击子项,删除子项

可悲的是,我只到了第 1 步。我已经弄清楚了如何在单击父影片剪辑时播放声音(我正在使用链接),但是当我尝试与孩子进行相同操作时,我收到以下错误:

TypeError:错误#1010:术语未定义且没有属性。(我不再收到此错误)

场景 1,图层“动作”,第 1 帧,第 29 行 1120:访问未定义的属性 newBox。

    leftBox.addEventListener(MouseEvent.CLICK, addBox);
    函数添加框(事件:鼠标事件):无效

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

    }
    newBox.addEventListener(MouseEvent.CLICK, playSound);
    函数播放声音(事件:事件)
    {
    var mySound:testSound = new testSound();
    mySound.play();

    }

任何帮助将非常感激。

谢谢!

(PS我是n00b,所以拜托,乖一点!)

4

1 回答 1

1

您正在尝试在 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 
 }
于 2013-04-05T21:07:31.537 回答