title: Drag and Drop Game using AS3
body: <pre><code>import flash.events.MouseEvent;
  import flash.utils.Timer;
  import flash.events.TimerEvent;
  
  var wordArray:Array = [mc_clipA, mc_clipB];
  var imgArray:Array = [Aimg, Bimg];
  var posArray:Array = [ {x:816.15, y:396.05}, {x:518.3, y:410.05}];
  
  var ClipA:MovieClip;
  var ClipB:MovieClip;
  
  var startXpositionClipA:Number;
  var startYpositionClipA:Number;
  var startXpositionClipB:Number;
  var startYpositionClipB:Number;
  
  wordArray[0].buttonMode = true;
  wordArray[1].buttonMode = true;
  
  wordArray[0].addEventListener(MouseEvent.MOUSE_DOWN, item_onMouseDownClipA);
  wordArray[1].addEventListener(MouseEvent.MOUSE_DOWN, item_onMouseDownClipB);
  
  //Mouse Down Function for ClipA
  function item_onMouseDownClipA(event:MouseEvent):void {
      ClipA = MovieClip(event.currentTarget);
      startXpositionClipA = ClipA.x;
      startYpositionClipA = ClipA.y;
      addChild(ClipA); 
      ClipA.startDrag();
      stage.addEventListener(MouseEvent.MOUSE_UP, stage_onMouseUpClipA);
      wordArray[0].removeEventListener(MouseEvent.MOUSE_DOWN, item_onMouseDownClipA);
      wordArray[1].removeEventListener(MouseEvent.MOUSE_DOWN, item_onMouseDownClipB);
  }
  
  //Mouse Up Function for ClipA
  function stage_onMouseUpClipA(event:MouseEvent):void {
      stage.removeEventListener(MouseEvent.MOUSE_UP, stage_onMouseUpClipA);
      ClipA.stopDrag();
      var indexClipA:int = wordArray.indexOf(ClipClipA);
      var matchClipClipA:MovieClip = MovieClip(imageArray[indexClipA]);
      if(matchClipClipA.hitTestPoint(ClipA.x, ClipA.y, true)) {
          ClipA.x = positionArray[indexClipA].x;
          ClipA.y = positionArray[indexClipA].y;
          ClipA.removeEventListener(MouseEvent.MOUSE_DOWN, item_onMouseDownZip);
          ClipA.buttonMode = false;
          var tot=0; 
          tot=tot+10;
          score.text="You scored: " + tot +" Points ";
          var setTimer:Timer = new Timer(1000,60);
          setTimer.addEventListener(TimerEvent.TIMER,doTask);
          setTimer.start();
          function doTask(event:TimerEvent) {
          gotoAndStop(1,"Scene 2");
          setTimer.stop();
          }
      } 
      else
      {    
          ClipA.x = startXpositionClipA;
              ClipA.y = startYpositionClipA;
          wordArray[0].addEventListener(MouseEvent.MOUSE_DOWN, item_onMouseDownClipA);
          wordArray[1].addEventListener(MouseEvent.MOUSE_DOWN, item_onMouseDownClipB);
  
      }
  }
  
  //Mouse Down Function for ClipB
  function item_onMouseDownClipB(event:MouseEvent):void {
      ClipB = MovieClip(event.currentTarget);
      startXpositionClipB = ClipB.x;
      startYpositionClipB = ClipB.y;
      addChild(ClipB); 
      ClipB.startDrag();
      stage.addEventListener(MouseEvent.MOUSE_UP, stage_onMouseUpClipB);
      wordArray[0].removeEventListener(MouseEvent.MOUSE_DOWN, item_onMouseDownClipA);
      wordArray[1].removeEventListener(MouseEvent.MOUSE_DOWN, item_onMouseDownClipB);
  
  }
  
  //Mouse Up Function for ClipB
  function stage_onMouseUpClipB(event:MouseEvent):void {
      stage.removeEventListener(MouseEvent.MOUSE_UP, stage_onMouseUpClipB);
      ClipB.stopDrag();
      var indexClipB:int = wordArray.indexOf(ClipB);
      var matchClipClipB:MovieClip = MovieClip(imageArray[indexClipB]);
      if(matchClipClipB.hitTestPoint(ClipB.x, ClipB.y, true)) {
          ClipB.x = positionArray3[indexTie].x;
          ClipB.y = positionArray3[indexTie].y;
          ClipB.removeEventListener(MouseEvent.MOUSE_DOWN, item_onMouseDownClipB);
          ClipB.buttonMode = false;
          var tot=0; 
          tot=tot;
          score.text="You scored: " + tot +" Points ";
          var setTimer:Timer = new Timer(1000,60);
          setTimer.addEventListener(TimerEvent.TIMER,doTask);
          setTimer.start();
          function doTask(event:TimerEvent) {
          gotoAndStop(1,"Scene 2");
          setTimer.stop();
          }
      } 
      else
      {    
          ClipB.x = startXpositionClipB;
              ClipB.y = startYpositionClipB;
          wordArray[0].addEventListener(MouseEvent.MOUSE_DOWN, item_onMouseDownClipA);
          wordArray[1].addEventListener(MouseEvent.MOUSE_DOWN, item_onMouseDownClipB);
      }
  }
  </code></pre>
  
  <p>This is regarding a drag n drop game. A particular object name will be displayed to the user then the user has to drag n drop the correct image to specified position and once the task is completed it will dynamically navigate to the next scene. For that i have used the timer class. I am getting this error in the scene 2 but in scene 1 the code runs perfectly.</p>
  
  <p>The naming conventions are all correct but i still get the following error,</p>
  
  <p>TypeError: Error #1009: Cannot access a property or method of a null object reference.
      at MethodInfo-1156()
      at flash.utils::Timer/_timerDispatch()
      at flash.utils::Timer/tick()</p>
  
  <p>Can someone help me with this.</p>