forked from: Chapter 21 Example 13

by actionscriptbible forked from Chapter 21 Example 13 (diff: 48)
Just for fun... Push around the instances 
by clicking and dragging on the stage.
♥0 | Line 100 | Modified 2009-08-28 05:10:22 | 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/e6N2
 */

//Just for fun... Push around the instances 
//by clicking and dragging on the stage.
package {
  import flash.display.DisplayObject;
  import flash.display.Sprite;
  import flash.events.ContextMenuEvent;
  import flash.events.MouseEvent;
  import flash.geom.Point;
  import flash.ui.ContextMenu;
  import flash.ui.ContextMenuItem;

  [SWF(backgroundColor="0")]
  public class ch21ex13 extends Sprite {
    public function ch21ex13() {
      var circ:DraggableCircle = new DraggableCircle();
      circ.x = circ.y = 100;
      addChild(circ);
      
      stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);

      var menu:ContextMenu = new ContextMenu();
      menu.hideBuiltInItems();
      var item:ContextMenuItem;
      item = new ContextMenuItem("Arrange all");
      item.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, onArrange);
      menu.customItems.push(item);
      item = new ContextMenuItem("Create 20 more");
      item.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, onCreate);
      menu.customItems.push(item);
      contextMenu = menu;
    }
    protected function onArrange(event:ContextMenuEvent):void {
      for (var i:int = 0, x:Number = 50, y:Number = 50; i < numChildren; i++) {
        var circ:DisplayObject = getChildAt(i);
        circ.x = x;
        circ.y = y;
        if ((x += 50) > stage.stageWidth) {
          x = 0;
          y += 50;
        }
      }
    }
    protected function onCreate(event:ContextMenuEvent):void {
      for (var i:int = 0; i < 20; i++) {
        var circ:DraggableCircle = new DraggableCircle();
        circ.x = stage.mouseX + Math.random()*50-25;
        circ.y = stage.mouseY + Math.random()*50-25;
        addChild(circ);
      }
    }
    protected function onMouseMove(event:MouseEvent):void {
      if (!event.buttonDown) return;
      var cursorPoint:Point = new Point(stage.mouseX, stage.mouseY);
      for (var i:int = 0; i < numChildren; i++) {
        var circ:DisplayObject = getChildAt(i);
        var p:Point = circ.localToGlobal(new Point());
        p = p.subtract(cursorPoint);
        var len:Number = 400 * Math.pow(p.length, -1.5);
        p.normalize(1);
        circ.x += p.x * len;
        circ.y += p.y * len;
      }
    }
  }
}
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.ui.ContextMenu;
import flash.ui.ContextMenuItem;
import flash.events.ContextMenuEvent;
import flash.display.BlendMode;

class DraggableCircle extends Sprite {
  public function DraggableCircle() {
    graphics.beginFill(makeColor(), 0.5);
    graphics.drawCircle(0, 0, 50);
    graphics.endFill();
    addEventListener(MouseEvent.MOUSE_DOWN, onStartDrag);
    buttonMode = true;
    blendMode = BlendMode.ADD;
    
    var menu:ContextMenu = new ContextMenu();
    menu.hideBuiltInItems();
    var item:ContextMenuItem = new ContextMenuItem("Clone");
    item.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, onClone);
    menu.customItems.push(item);
    contextMenu = menu;
  }
  protected function makeColor():uint {
    var rnd:Function = function():uint{return uint(Math.random() * 256)};
    return (rnd() << 16 | rnd() << 8 | rnd());
  }
  protected function onStartDrag(event:MouseEvent):void {
    startDrag();
    stage.addEventListener(MouseEvent.MOUSE_UP, onStopDrag);
  }
  protected function onStopDrag(event:MouseEvent):void {
    stage.removeEventListener(MouseEvent.MOUSE_UP, onStopDrag);
    stopDrag();
  }
  public function onClone(event:ContextMenuEvent):void {
    var copy:DraggableCircle = new DraggableCircle();
    copy.x = this.x + 10;
    copy.y = this.y + 10;
    this.parent.addChild(copy);
  }
}