0
votes

In short, here is what I'd like to accomplish:

  1. Click Movie Clip, add child
  2. Click child movie clip, play sound
  3. Click child again, stop sound
  4. Click child a third time, remove child

Sadly, I've only gotten as far as step 1. I've figured out how to get a sound to play when the parent movie clip is clicked (I'm using linkage), but when I attempt the same after with the child, I get the following error:

TypeError: Error #1010: A term is undefined and has no properties. (I'm no longer getting this error)

Scene 1, Layer 'actions', Frame 1, Line 29 1120: Access of undefined property 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();

    }

Any help would be much appreciated.

Thanks!

(P.S. I'm a n00b, so please, be nice!)

1
Please add your code snippet which you think is problematic to the question.Ihsan
@Ihsan, I just added the snippet. But the problem is definitely the playSound function. Everything works fine when I comment it out.user2247402

1 Answers

1
votes

You are trying to add an event listener to newbox, before it is created.. Try it as follows:

// 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 
 }