In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

マウスドラッグ&ドロップによる範囲指定

マウスドラッグでラインを描画、ドロップでラインで囲った範囲に図形を
作成する
*作成した図形はドラッグ&ドロップで移動させることができる
Get Adobe Flash player
by behist 17 May 2010
    Embed
/**
 * Copyright behist ( http://wonderfl.net/user/behist )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/uDMF
 */

// forked from behist's flash on 2010-5-16
/*
*マウスドラッグでラインを描画、ドロップでラインで囲った範囲に図形を
作成する
*作成した図形はドラッグ&ドロップで移動させることができる
*/
package {
    import flash.display.Sprite;
    import flash.events.MouseEvent;
    import flash.geom.Point;
    
    public class FlashTest extends Sprite {
    		private var canvas:Sprite = new Sprite;
    		private var dragMode:Boolean = false;
    		private var pointArray:Array = new Array;
        public function FlashTest() {
            // write as3 code here..
            addChild(canvas);
            canvas.graphics.beginFill(0xFFFFFF);
            canvas.graphics.drawRect(0,0,500,500);
            canvas.addEventListener(MouseEvent.MOUSE_DOWN,onMouseDown);
            canvas.addEventListener(MouseEvent.MOUSE_MOVE,onMouseMove);
            canvas.addEventListener(MouseEvent.MOUSE_UP,onMouseUp);
        
       }
        private function onMouseDown(ev:MouseEvent):void{
        		dragMode = true;
        		canvas.graphics.lineStyle(1,0x111133);
        		canvas.graphics.moveTo(mouseX,mouseY);
        		canvas.graphics.lineTo(mouseX,mouseY);
        		pointArray.push(new Point(mouseX,mouseY));
        }
         private function onMouseMove(ev:MouseEvent):void{
        		if(dragMode){
        			canvas.graphics.lineTo(mouseX,mouseY);
        			pointArray.push(new Point(mouseX,mouseY));
        		}
        }
         private function onMouseUp(ev:MouseEvent):void{
         	var sprite:Sprite = new Sprite;
         	sprite.addEventListener(MouseEvent.MOUSE_DOWN,onDragStart);
         	sprite.addEventListener(MouseEvent.MOUSE_UP,onDragEnd);
         	addChild(sprite);
         	
         	sprite.graphics.beginFill(0x225566,0.5);
         	sprite.graphics.moveTo(pointArray[0].x,pointArray[0].y);
         	for each(var point:Point in pointArray){
         		sprite.graphics.lineTo(point.x,point.y);
         	}
         	//描画処理系統初期化
         	canvas.graphics.clear();
         	canvas.graphics.beginFill(0xFFFFFF);
         	canvas.graphics.drawRect(0,0,500,500);
         	dragMode = false;
         	pointArray = new Array;
        }
        private function onDragStart(ev:MouseEvent):void{
        		ev.target.startDrag();
        }
        private function onDragEnd(ev:MouseEvent):void{
        		ev.target.stopDrag();
        }
    }
}