180bpm

점선 그리는 클래스 본문

Flash/AS2

점선 그리는 클래스

powdersnow 2012. 2. 1. 12:21
Main.as
import DotLine;

/**
 * 점선을 그려주는 클래스
 * AS 2.0으로 바꿨다.
 * @author deb
 */
class Main 
{
	/**
	 * 생성자를 대체하는 함수
	 * @param	swfRoot
	 */
	//private  dLine:DotLine;
	public static function main(swfRoot:MovieClip):Void 
	{
		// entry point
		
		var mc:MovieClip = new MovieClip();
		mc = swfRoot.createEmptyMovieClip('mcLine', 1);
		
		var dLine = new DotLine();
		dLine.lineStyle(mc,32);
		dLine.dotStyle();
		dLine.lineTo(100, 200);
		
	}
	

	/**
	 * 생성자
	 * 사용되지 않는다
	 */
	public function Main() 
	{
		
	}
	
	
}


DotLine.as 
import flash.display.*;
import flash.geom.*;
import mx.data.encoders.Num;
/**
 * ...
 * @author deb
 */
class DotLine extends MovieClip
{
	private var _graphics:MovieClip;
	private var _matrix:Matrix = new Matrix();
	private var _colors:Array=[0, 0, 0, 0];
	private var _alphas:Array=[0, 0, 0, 0];
	private var _ratios:Array=[0, 0, 0, 0xFF];
	private var _px:Number = 0;
	private var _py:Number = 0;
	private	var _line:Number = 4;
	private var _spacing:Number = 4;
	private var _lineLength:Number = 8;
	
	private var isDebug:Boolean = false;
		
	public function DotLine() 
	{
		
		if(isDebug) trace("create DotLine Class");	
	}
	
	/**
	 * 선의 속성을 지정해주는 메서드
	 * @param	$graphics	그림이 그려질 대상
	 * @param	$thickness	두께	NaN
	 * @param	$color		색		0
	 * @param	$alpha		투명도	100
	 * @param	$pixelHinting	외각선을 정리할 것인가	false
	 * @param	$noScale		스케일에 따라 선 두께도 굵어질것인가 normal, none, vertical
	 * @param	$capsStyle		선 끝을 둥글게 처리할것인가, 아니면 감쌀것인가 null
	 * @param	$jointStyle		선이 꺾이면 어떻게 처리할것인가 null
	 * @param	$miterLimit		$jointStyle 속성을 사용하면 어느정도 길이에서 자를것인가 3
	 * @see	http://help.adobe.com/ko_KR/FlashPlatform/reference/actionscript/3/flash/display/Graphics.html#lineStyle()
	 */
	public function lineStyle($graphics:MovieClip, $thickness:Number, $color:Number, $alpha:Number,
	$pixelHinting:Boolean,$noScale:String, $capsStyle:String, $jointStyle:String, $miterLimit:Number):Void 
	{
		if(isDebug) trace("into lineStyle method");
		_graphics = $graphics;
		trace(_graphics);

		/*if (!$thickness) 	{	$thickness = NaN;		}
		if (!$color) 		{	$color = 0;				}
		if (!$alpha) 		{	$alpha = 100;			}
		if (!$pixelHinting) {	$pixelHinting = false;	}
		if (!$noScale) 		{	$noScale = "normal";	}
		if (!$capsStyle)	{	$capsStyle = null;		}
		if (!$jointStyle) 	{	$jointStyle = null;		}
		if (!$miterLimit) 	{	$miterLimit = 3;		}*/
		$thickness 		= 		$thickness || NaN;
		$color			=		$color || 0;
		$alpha			=		$alpha || 100;
		$pixelHinting	=		$pixelHinting || false;
		$noScale		=		$noScale || "normal";
		$capsStyle		=		$capsStyle || "none";
		$jointStyle		=		$jointStyle || null;
		$miterLimit		=		$miterLimit || 3;
		
		_colors[0] = $color;
		_colors[1] = $color;
		_colors[2] = $color;
		_colors[3] = $color;
		
		_alphas[0] = $alpha;
		_alphas[1] = $alpha;
		if (_graphics) 
		{
				_graphics.lineStyle($thickness, $color, $alpha, $pixelHinting, $noScale, $capsStyle, $jointStyle, $miterLimit);
		}//if
	}
	
	/**
	 * 점선의 속성을 지정해주는 메서드
	 * @param	$line		선이 보이는 길이
	 * @param	$spacing	선이 안보이는 길이
	 */
	public function dotStyle($line:Number, $spacing:Number):Void 
	{
		if(isDebug) trace("into dotStyle method");
		if (!$line) 	{	$line = 4;		}
		if (!$spacing) 	{	$spacing = 4;	}
		_line = $line;
		_spacing = $spacing;
		_lineLength = _line + _spacing;
		
		//line과 spaceing을 합한 값에서 Line의 비율을 얻어 255를 곱한다
		var n:Number = $line / ($line + $spacing) * 0xFF;
		
		
		_ratios[1] = n - 0.1;
		_ratios[2] = n + 0.1;
	}
	private var _decx:Number = 0;
	private var _decy:Number = 0;
	/**
	 * 시작점을 이동하는 메서드
	 * @param	$x
	 * @param	$y
	 */
	public function moveTo($x:Number, $y:Number):Void 
	{
		if (isDebug) trace("into moveTo method");
		_px = $x || 0;
		_py = $y || 0;
		_decx = 0;
		_decy = 0;
		_graphics.moveTo(_px,_py);
		
	}
	
	public function lineTo($x:Number, $y:Number):Void 
	{
		var x:Number, y:Number, r:Number;
		x = $x - _px;
		y = $y - _py;
		
		_decx = (x % _lineLength)*2/2;
		_decy = (y % _lineLength)*2/2;
		trace(y % _lineLength);
		
		//http://gall.dcinside.com/list.php?id=flash&no=1296
		r = Math.atan2(y, x);
		
		x = $x + Math.cos(r) * x;
		y = $y + Math.sin(r) * y;
		
		/*그래디언트 박스를 만든다
		 * 가로,세로, 각도, 이동 거리, 이동거리
		 * 그러니까 시작점은 해당 선의 시작점이고 그 주위로 repeat 하면서 뿌려지는 방식이다.
		 */
		_matrix.createGradientBox(_lineLength, _lineLength, r , _px - _decx,  _py - _decy);
		if (isDebug) {
			trace("Colors : " + _colors);
			trace("_alphas : " + _alphas);
			trace("Colors : " + _ratios);
			trace("_matrix : " + _matrix.toString() );
			
		}
		
		//trace ("createGradientBox : " + x + ",	" + y + ",	" + r + ",	" + $x + ",	" + $y + ",	" + _px + ",	" + _py + ",	" + _decx + ",	" + _decy);
		_graphics.lineGradientStyle("linear", _colors, _alphas, _ratios, _matrix, "repeat", "linearRGB", 0);
		_graphics.lineTo($x, $y);		
		_px = $x;
		_py = $y;
		
		
	}
}


 


Comments