1 Reply Latest reply on Jul 20, 2007 12:56 PM by Steve_Butcher

    problems with startDrag()

    Steve_Butcher
      Has anyone else had problems with Sprite#startDrag() when using the optional parameters?

      According to the documentation, I should be able to compile something like:

      tile.startDrag( true, tile.parent.getBounds());

      This behavior is documented as:

      public function startDrag(lockCenter:Boolean = false, bounds:Rectangle = null):void

      where the first parameter says "center the sprite on the mouse" rather than where the use clicked and the second parameter constrains the movement of the sprite to the bounding box, measured in the Sprite's parent's coordinates.

      However, when I go to compile this, the first error I get is that Sprite#startDrag() only accepts one argument:

      mover.mxml(17): Error: Incorrect number of arguments. Expected 1.

      t.startDrag( false, t.parent.getBounds());

      Ooops...which one? It turns out it only takes the less useful one (lockCenter). However, when I try:

      tile.startDrag( true);

      I do not get the described behavior at all. Instead, the mouse is locked to 0,0 on the Sprite and the mouseUp will not work at all (the Sprite gets "stuck" to the mouse). If I go back to:

      tile.startDrag();

      everything works as expected but I still can drag the Sprite anywhere I want. This is not the desired behavior. Here is some code to test that this is the case on other systems, here:


      <?xml version="1.0"?>
      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" width="600" height="400" creationComplete="init();">
      <mx:Script>
      <![CDATA[
      import flash.events.MouseEvent;

      public function init(): void {
      tile.addEventListener( MouseEvent.MOUSE_DOWN, handleMouseDown);
      tile.addEventListener( MouseEvent.MOUSE_UP, handleMouseUp);
      }

      public function handleMouseDown( event: MouseEvent): void {
      var t : Canvas = Canvas( event.target);
      t.addEventListener( MouseEvent.MOUSE_MOVE, handleMouseMove);
      // t.startDrag( true); // the default is false
      t.startDrag( false, t.parent.getBounds());
      // t.startDrag();
      }

      public function handleMouseUp( event: MouseEvent): void {
      var t : Canvas = Canvas( event.target);
      t.removeEventListener( MouseEvent.MOUSE_MOVE, handleMouseMove);
      t.stopDrag();
      }

      public function handleMouseMove( event: MouseEvent): void {
      var t : Canvas = Canvas( event.target);
      event.updateAfterEvent();
      }
      ]]>
      </mx:Script>

      <mx:Panel id="pallette" height="100%" width="100%" clipContent="true">
      <mx:Canvas id="tile" x="25" y="25" width="25" height="25" backgroundColor="red"/>
      </mx:Panel>
      </mx:Application>