The Easiest Way to Save and Share Code Snippets on the web

Draw Dashed Lines Horizontal and Vertical [Flex/AS3]

actionscript3 | by: danosphere

posted: Mar, 12th 2009 | jump to bottom

package com.dlw.common.graphics {
 
	import flash.geom.Point;
 
	public class DashedLine {
 
		public var dashLen:int;
		public var spaceLen:int;
		public var lineThickness:int;
		public var lineColor:uint;
 
		public function DashedLine(dashLen:int=5, spaceLen:int=5, lineThickness:int=1, lineColor:uint=0x000000) {
			this.dashLen = dashLen;
			this.spaceLen = spaceLen;
			this.lineThickness = lineThickness;
			this.lineColor = lineColor;
		}
 
		public function drawDashedLine(target:*, startPoint:Point, endPoint:Point):void {
			var complete:Boolean = false;
			var dLen:int = this.dashLen;
			var sLen:int = this.spaceLen;
			var tempVal:int = 0;
 
			//ONE WAY DRAWING!!
			if (startPoint.y == endPoint.y) { //drawing on the same y axis, next point will be x-based
				if (startPoint.x > endPoint.x) { //always draw from the smaller val to the larger (flip vals)
					tempVal = startPoint.x;
					startPoint.x = endPoint.x;
					endPoint.x = tempVal;
				}
			}else{ //drawing on same x axis, next point will be y-based
				if (startPoint.y > endPoint.y) { //always draw from the smaller val to the larger (flip vals)
					tempVal = startPoint.y;
					startPoint.y = endPoint.y;
					endPoint.y = tempVal;
				}
			}
			//============
 
 
			target.graphics.lineStyle(this.lineThickness, this.lineColor, 1);
			target.graphics.moveTo(startPoint.x, startPoint.y); //move to the start point
 
			while (!complete) {
				//check to see if the next dash draw will exceed the distance
				//and if so, shrink the dashlen
				if (startPoint.y == endPoint.y) {
					if (startPoint.x + dLen >= endPoint.x) {
						dLen -= ((startPoint.x + dLen) - endPoint.x);
						complete = true;
					}
 
					target.graphics.lineTo(startPoint.x + dLen, startPoint.y);
					startPoint.x += dLen;
 
 
					if (startPoint.x + sLen >= endPoint.x) {
						sLen -= ((startPoint.x + sLen) - endPoint.x);
 
					}
 
					target.graphics.moveTo(startPoint.x + sLen, startPoint.y);
					startPoint.x += sLen;
 
				}else{
					if (startPoint.y + dLen >= endPoint.y) {
						dLen -= ((startPoint.y + dLen) - endPoint.y);
						complete = true;
					}
 
					target.graphics.lineTo(startPoint.x, startPoint.y + dLen);
					startPoint.y += dLen;
 
 
					if (startPoint.y + sLen >= endPoint.y) {
						sLen -= ((startPoint.y + sLen) - endPoint.y);
 
					}
 
					target.graphics.moveTo(startPoint.x, startPoint.y + sLen);
					startPoint.y += sLen;
				}
			}
		}
	}
}
259 views