Chapter 21 Example 6

by actionscriptbible
♥0 | Line 40 | Modified 2009-08-27 07:17:51 | MIT License
play

ActionScript3 source code

/**
 * Copyright actionscriptbible ( http://wonderfl.net/user/actionscriptbible )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/v7pH
 */

package {
  import flash.display.Sprite;
  public class ch21ex6 extends Sprite {
    public function ch21ex6() {
      var circ:DraggableCircle = new DraggableCircle();
      circ.x = circ.y = 100;
      addChild(circ);
    }
  }
}
import flash.display.Sprite;
import flash.events.MouseEvent;
class DraggableCircle extends Sprite {
  public function DraggableCircle() {
    graphics.beginFill(0, 0.5);
    graphics.drawCircle(0, 0, 50);
    graphics.endFill();
    addEventListener(MouseEvent.MOUSE_DOWN, onStartDrag);
    buttonMode = true;
  }
  protected function onStartDrag(event:MouseEvent):void {
    if (event && event.shiftKey) {
      cloneAndDrag();
    } else {
      startDrag();
      stage.addEventListener(MouseEvent.MOUSE_UP, onStopDrag);
    }
  }
  protected function onStopDrag(event:MouseEvent):void {
    stage.removeEventListener(MouseEvent.MOUSE_UP, onStopDrag);
    stopDrag();
  }
  protected function cloneAndDrag():void {
    var copy:DraggableCircle = new DraggableCircle();
    copy.x = this.x;
    copy.y = this.y;
    this.parent.addChild(copy);
    copy.onStartDrag(null);
  }
}