Chapter 21 Example 13

by actionscriptbible
♥0 | Line 64 | Modified 2012-03-27 18:48:40 | 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/cbxp
 */

package {
  import flash.display.*;
  import flash.events.*;
  import flash.geom.Point;
  import flash.ui.*;
  [SWF(backgroundColor="#000000")]
  public class ch21ex13 extends Sprite {
    public function ch21ex13() {
      var circ:DraggableCircle = new DraggableCircle();
      circ.x = circ.y = 100;
      addChild(circ);
      
      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);
      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;}
      }
    }
  }
}
import flash.display.*;
import flash.events.*;
import flash.ui.*;
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);
  }
}

Forked