ActionScript로 점선 그리기

작성자

카테고리:

이전 포스트와 마찬가지로, 이미지 에디터에서 필요한 기능 구현하면서 만들어 본 샘플입니다. 이번에는 점선 혹은 대쉬선(dashed line)을 그리는 기능입니다.

import flash.display.*;
import flash.geom.Rectangle;
import flash.events.MouseEvent;
import flash.text.*;

var fmt:TextFormat = new TextFormat("Arial", 14, 0x666666);
var tf:TextField = new TextField();
tf.defaultTextFormat = fmt;
tf.autoSize = TextFieldAutoSize.LEFT;
tf.text = "Drag on the stage";
tf.selectable = false;
tf.x = (stage.stageWidth - tf.width) / 2;
tf.y = (stage.stageHeight - tf.height) / 2;
addChild(tf);

// horizontal line source bitmapdata
var hlineBD:BitmapData = new BitmapData(6, 1, true, 0x0);
hlineBD.fillRect(new Rectangle(0, 0, 3, 1), 0xFF000000);
// vertical line source bitmapdata
var vlineBD:BitmapData = new BitmapData(1, 6, true, 0x0);
vlineBD.fillRect(new Rectangle(0, 0, 1, 3), 0xFF000000);

var sp:Sprite = new Sprite();
sp.x = sp.y = 20;
addChild(sp);

function drawLine(w:int, h:int):void
{
	var grp:Graphics = sp.graphics;
	grp.clear();
	
	// draw horizontal lines
	grp.lineStyle(1);
	grp.lineBitmapStyle(hlineBD);
	grp.moveTo(0, 0);
	grp.lineTo(w, 0);
	grp.moveTo(w, h);
	grp.lineTo(0, h);
	grp.endFill();
	
	// draw vertical lines
	grp.lineStyle(1);
	grp.lineBitmapStyle(vlineBD);
	grp.moveTo(w, 0);
	grp.lineTo(w, h);
	grp.moveTo(0, h);
	grp.lineTo(0, 0);
	grp.endFill();
}
drawLine(560, 360);


stage.addEventListener(MouseEvent.MOUSE_DOWN, downListener);

function downListener(e:MouseEvent):void
{
	stage.addEventListener(MouseEvent.MOUSE_MOVE, moveListener);
	stage.addEventListener(MouseEvent.MOUSE_UP, upListener);
	moveListener();
}
function moveListener(e:MouseEvent=null):void
{
	drawLine(sp.mouseX, sp.mouseY);
}
function upListener(e:MouseEvent):void
{
	stage.removeEventListener(MouseEvent.MOUSE_MOVE, moveListener);
	stage.removeEventListener(MouseEvent.MOUSE_UP, upListener);
	moveListener();
}

코멘트

2의 “ActionScript로 점선 그리기”에 대한 답변

  1. bemga 아바타
    bemga

    이렇게 훌륭한 점선 그리기 방법이 있다니…

    많이 배우고 갑니다 ^^

    1. Hangun's World 아바타

      저도 외국 블로그에서 보고 힌트를 얻었어요 ^^

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다