title: Issue with removing eventlistener in actionscript
body: <p>I'm not new to flash, but I'm a bit of a noob with actionscript, trying to build an app in flash pro (or rather, animate cc) which will (hopefully) teach the users music theory (how to read music, etc.). What I want is to have different lessons on separate frames, with separate "screens" which the user can swipe through. I'm using multiple copies of the swipe code which adobe provides in their swipe gallery template.</p>
  
  <p>On frame 5, I use the following:</p>
  
  <pre><code>stop()
  
  Multitouch.inputMode = MultitouchInputMode.GESTURE;
  
  var currentGalleryItem:Number = 1;
  var totalGalleryItems:Number = 10;
  
  stage.addEventListener (TransformGestureEvent.GESTURE_SWIPE, fl_SwipeToGoToNextPreviousFrameB);
  
  function fl_SwipeToGoToNextPreviousFrameB(event:TransformGestureEvent):void
  {
      if(event.offsetX == 1)
      {
          if(currentGalleryItem &gt; 1){
              currentGalleryItem--;
              slideRight();
          }
      }
      else if(event.offsetX == -1)
      {
          if(currentGalleryItem &lt; totalGalleryItems){
              currentGalleryItem++;
              slideLeft();
          }
      }
  }
  var slideCounter:Number = 0;
  function slideLeft(){
      lsn112.addEventListener("enterFrame", moveGalleryLeft);
  }
  function slideRight(){
      lsn112.addEventListener("enterFrame", moveGalleryRight);
  }
  
  function moveGalleryLeft(evt:Event){
      lsn112.x -= 128;
      slideCounter++;
      if(slideCounter == 10){
          lsn112.removeEventListener("enterFrame", moveGalleryLeft);
          slideCounter = 0;
      }
  }
  function moveGalleryRight(evt:Event){
      lsn112.x += 128;
      slideCounter++;
      if(slideCounter == 10){
          lsn112.removeEventListener("enterFrame", moveGalleryRight);
          slideCounter = 0;
      }
  }
  
  Home112.addEventListener(MouseEvent.CLICK, fl_ClickToGoToAndStopAtFrame_22);
  
  function fl_ClickToGoToAndStopAtFrame_22(event:MouseEvent):void
  {
      gotoAndStop(2);
  }
  
  stop()
  </code></pre>
  
  <p>Frame 6 is almost identical, just with different names for variables, functions, etc.:</p>
  
  <pre><code>stop()
  
  Multitouch.inputMode = MultitouchInputMode.GESTURE;
  
  var currentGalleryItemA:Number = 1;
  var totalGalleryItemsA:Number = 11;
  
  stage.addEventListener (TransformGestureEvent.GESTURE_SWIPE, fl_SwipeToGoToNextPreviousFrameA);
  
  function fl_SwipeToGoToNextPreviousFrameA(event:TransformGestureEvent):void
  {
      if(event.offsetX == 1)
      {
          if(currentGalleryItemA &gt; 1){
              currentGalleryItemA--;
              slideRightA();
          }
      }
      else if(event.offsetX == -1)
      {
          if(currentGalleryItemA &lt; totalGalleryItemsA){
              currentGalleryItemA++;
              slideLeftA();
          }
      }
  }
  var slideCounterA:Number = 0;
  function slideLeftA(){
      lsn113.addEventListener("enterFrame", moveGalleryLeftA);
  }
  function slideRightA(){
      lsn113.addEventListener("enterFrame", moveGalleryRightA);
  }
  
  function moveGalleryLeftA(evt:Event){
      lsn113.x -= 128;
      slideCounterA++;
      if(slideCounterA == 10){
          lsn113.removeEventListener("enterFrame", moveGalleryLeftA);
          slideCounterA = 0;
      }
  }
  function moveGalleryRightA(evt:Event){
      lsn113.x += 128;
      slideCounterA++;
      if(slideCounterA == 10){
          lsn113.removeEventListener("enterFrame", moveGalleryRightA);
          slideCounterA = 0;
      }
  }
  
  Home113.addEventListener(MouseEvent.CLICK, fl_ClickToGoToAndStopAtFrame_23);
  
  function fl_ClickToGoToAndStopAtFrame_23(event:MouseEvent):void
  {
      gotoAndStop(2);
  }
  
  stop()
  </code></pre>
  
  <p>There is also a button as part of the movieclip "lsn112" which is being swiped. Don't know if this is relevant or not, but the code is:</p>
  
  <pre><code>stop();
  
  fwdtest.addEventListener(MouseEvent.CLICK, GoRootNext112);
  
  function GoRootNext112(event:MouseEvent):void
  {
      MovieClip(root).nextFrame();
  }
  </code></pre>
  
  <p>It works fine to a point, but I think an eventlistener is not being removed properly. When the user swipes through the gallery, it works as expected. They can then move onto the next gallery, which also works as expected. No errors so far. However, if they then go back to the menu, and then back to the gallery, I get an error code 1009:</p>
  
  <blockquote>
    <p>TypeError: Error #1009: Cannot access a property or method of a null
    object reference.     at
    MusicTheorySwipe_fla::MainTimeline/slideRightA()[MusicTheorySwipe_fla.MainTimeline::frame6:32]
      at
    MusicTheorySwipe_fla::MainTimeline/fl_SwipeToGoToNextPreviousFrameA()[MusicTheorySwipe_fla.MainTimeline::frame6:16]
      at runtime::ContentPlayer/simulationSendGestureEvent()  at
    runtime::SimulatedContentPlayer/clientSocketDataHandler()</p>
  </blockquote>
  
  <p>What confuses me is that I am using frame 5 at this point, yet I get an error referencing frame 6. It appears to me that flash is attempting to send a gesture to the eventlistener in frame 6, even though I'm on frame 5, which I'm guessing is down to an eventlistener not being removed. However, being new to code, I don't know when to remove the eventlistener without breaking the code.</p>
  
  <p>Here's a link to a zip containing the relevant .fla, .swf and .xml files.
  <a href="http://speedy.sh/5JP7c/MusicTheorySwipe.zip" rel="nofollow">http://speedy.sh/5JP7c/MusicTheorySwipe.zip</a></p>
  
  <p>As this is the method I would like to use over many, many frames, I would really appreciate your time and help in resolving this.</p>
  
  <p><strong><em>EDIT</em></strong></p>
  
  <p>Ok, I've simplified the code as best I can, to try and eliminate any suspects.</p>
  
  <p>Frame 5:</p>
  
  <pre><code>Multitouch.inputMode = MultitouchInputMode.GESTURE;
  
  stage.addEventListener(TransformGestureEvent.GESTURE_SWIPE , onSwipeA);
  var currentGalleryItemA:Number = 1;
  var totalGalleryItemsA:Number = 5;
  function onSwipeA (e:TransformGestureEvent):void{
  
  //User swiped towards right
  if (e.offsetX == 1) {
      if(currentGalleryItemA &gt; 1){
          currentGalleryItemA--;
          lsn113.x += 1280;
      }
  }
  
  //User swiped towards left
  if (e.offsetX == -1) {
      if(currentGalleryItemA &lt; totalGalleryItemsA){
      currentGalleryItemA++;
      lsn113.x -= 1280;
          if(currentGalleryItemA == totalGalleryItemsA){
          nextFrame()
          }
      }
  }
  }
  stop();
  </code></pre>
  
  <p>Frame 6:</p>
  
  <pre><code>stage.removeEventListener(TransformGestureEvent.GESTURE_SWIPE , onSwipeA);
  
  Multitouch.inputMode = MultitouchInputMode.GESTURE;
  
  stage.addEventListener(TransformGestureEvent.GESTURE_SWIPE , onSwipeB);
  var currentGalleryItemB:Number = 1;
  var totalGalleryItemsB:Number = 11;
  function onSwipeB (e:TransformGestureEvent):void{
  
  //User swiped towards right
  if (e.offsetX == 1) {
      if(currentGalleryItemB &gt; 1){
          currentGalleryItemB--;
          lsn112.x += 1280;
      }
  }
  
  //User swiped towards left
  if (e.offsetX == -1) {
      if(currentGalleryItemB &lt; totalGalleryItemsB){
          currentGalleryItemB++;
          lsn112.x -= 1280;
      }
      if(currentGalleryItemB == totalGalleryItemsB){
          nextFrame()
      }
  }
  }
  
  stop();
  </code></pre>
  
  <p>And that's all the actionscript there is now, yet it's still not working. Any ideas?</p>