Solved

Custom Class for 'Dashed Line' in AS3

Posted on 2009-07-14
5
357 Views
Last Modified: 2013-11-11
Hi all, I am trying to create a custom class for Dashed Line.
I have the attached code in .as file and thr following code in .fla file :

import diagrammingtool.utils.DashedLine;
import flash.geom.Point;
var spt:Point = new Point(200,50);
var ept:Point = new Point(400,100);
var dl:DashedLine = new DashedLine(spt,ept,4,2);
addChild(dl);



There are no errors but the theres no dashed line appearing either. Can any one point me to the mistake I am making ? Thanks.
package diagrammingtool.utils{
       import flash.geom.Point;
	   import flash.display.Graphics;
	   import flash.display.Sprite;
	   import flash.display.Shape;
 
 
        // START CLASS
        public class DashedLine extends Sprite{            
                // CONSTRUCTOR              
               private var startPoint:Point = new Point();
			   private var endPoint:Point = new Point();
               public function DashedLine(sPoint:Point, ePoint:Point, len:Number, gap:Number) {
				   var child:Shape = new Shape();
				   var seglen:Number = len + gap ;
				   var deltax = sPoint.x-ePoint.x; 
				   var deltay = sPoint.y-ePoint.y; 
				   var dist:Number = Math.sqrt(deltax*deltax+deltay*deltay);
				   var noOfSegs:Number =  Math.floor(Math.abs(dist/seglen));
				   var angle:Number = Math.atan2(deltay,deltax);
				   var cx:Number = sPoint.x;
				   var cy:Number = sPoint.y;
				   // add these to cx, cy to get next seg start
				   deltax = Math.cos(angle)*seglen;
				   deltay = Math.sin(angle)*seglen;
				   // loop through each seg
				   for (var n:Number = 0; n < noOfSegs; n++) {
						child.graphics.moveTo(cx,cy);
						child.graphics.lineTo(cx+Math.cos(angle)*len,cy+Math.sin(angle)*len);
						cx += deltax;
						cy += deltay;
						trace(cx);
				   }
				   // handle last segment as it is likely to be partial
				   child.graphics.moveTo(cx,cy);
				   var delta:Number = Math.sqrt((ePoint.x-cx)*(ePoint.x-cx)+(ePoint.y-cy)*(ePoint.y-cy));
				   if(delta>len){
					   // segment ends in the gap, so draw a full dash
					   child.graphics.lineTo(cx+Math.cos(angle)*len,cy+Math.sin(angle)*len);
				   } else if(delta>0) {
					   // segment is shorter than dash so only draw what is needed
					   child.graphics.lineTo(cx+Math.cos(angle)*delta,cy+Math.sin(angle)*delta);
				   }
				   // move the pen to the end position
				   child.graphics.moveTo(ePoint.x,ePoint.y);
				   addChild(child);
               }
 
 
        }
        // END CLASS
}
// END PACKAGE

Open in new window

0
Comment
Question by:whatisittellme
  • 3
  • 2
5 Comments
 
LVL 18

Accepted Solution

by:
Antonio Estrada earned 500 total points
ID: 24860308
Here's an old AS2 class I ported to AS3 also a while back. It works fine, so you might as well check it out:

http://vulturous.110mb.com/eefiles/DashedLine.as

-V
0
 
LVL 2

Author Comment

by:whatisittellme
ID: 24860625
Thank you. I tried using the one you sent with the following code on the .fla file. I might be doing something wrong, the dashed line doesnt show up :(



var dl = new DashedLine(000000,4,90,200,300,400,500,10,5);
stage.addChild(dl);

Open in new window

0
 
LVL 2

Author Comment

by:whatisittellme
ID: 24860726
oops, my mistake...it works. the had given the coordinates that were way off the stage. figured out. thanks !
0
 
LVL 18

Expert Comment

by:Antonio Estrada
ID: 24861465
Glad to help!

-V
0
 
LVL 2

Author Comment

by:whatisittellme
ID: 24879230
Hi 'Vulturous'. Could you take a look at this question: http://www.experts-exchange.com/Software/Photos_Graphics/Web_Graphics/Macromedia_Flash/ActionScript/Q_24579146.html

I have a similar problem but in this case I want to implement so that it can be overlayed on google map/microsoft live maps using UMap(it is an Advanced Flash Component available for free download). I would greatly appreciate any help. Thanks !
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SCCM 2012 - Deploy Flash 16 6,960
Problem witih Adobe Flash Player with a W7 machine 21 55
Download Adobe Flash Player 6 65
flash in chrome 4 101
Here are some practices and techniques that can be adopted into your Flash/Flex application development process. Note: Not all "performance tips" provide an immediately-recognizable benefit.   This article does not include timing validation data,…
The last time I worked with Flash and Socket connections was in AS1. A recent project required flash connecting to a Socket, and sending receiving information - we figured it would be easy enough - we all know about the socket policy documents and c…
In this tutorial viewers will learn how to create a basic motion tween animation in Flash Open a new document in Flash: Draw/import an image: Press CTRL + F8 to convert it into a graphic symbol: Select a frame (how long you want the tween to last): …
The goal of the tutorial is to teach the user how to select which audio input to use. Once you have an audio input plugged into the laptop or computer, you will go into the audio input settings and choose which audio input you want to use.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question