actionscript 3 - Add/Remove Event Listener to/from child Movie Clip in AS3 -


in short, here i'd accomplish:

  1. click movie clip, add child
  2. click child movie clip, play sound
  3. click child again, stop sound
  4. click child third time, remove child

sadly, i've gotten far step 1. i've figured out how sound play when parent movie clip clicked (i'm using linkage), when attempt same after child, following error:

typeerror: error #1010: term undefined , has no properties. (i'm no longer getting 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 appreciated.

thanks!

(p.s. i'm n00b, please, nice!)

you trying add event listener newbox, before created.. try follows:

// mysound should availible in scope var mysound:testsound = new testsound();  // newbox var newbox:right_box; // here channel var channel: soundchannel;  // ok adds 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;     // should add listener here...     newbox.addeventlistener(mouseevent.click, playsound);     // have avoid multiple newboxes on each other ,     // leaving older ones under..     // stop listening newbox generating event:      leftbox.removeeventlistener(mouseevent.click, addbox); }  function playsound(event:event){     channel = mysound.play();     // on next click want sound stop     // first remove old listener avoid play over:       newbox.removeeventlistener(mouseevent.click, playsound);     // , hook listener stop method     newbox.addeventlistener(mouseevent.click, stopsound);  }   function stopsound(event:event){     channel.stop();     // on next click want remove newbox      // first remove old listener avoid play over:       newbox.removeeventlistener(mouseevent.click, stopsound);     newbox.addeventlistener(mouseevent.click, removenewbox);  }   function removenewbox(event:event){     // first remove listener :       newbox.removeeventlistener(mouseevent.click, removenewbox );     removechild(newbox); // remove display list     newbox = null; // make contents eligible garbage collection   } 

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -