Drag draw sprite

I’ve had a search in the forums and I couldn’t find an answer.

I need help with creating a draw tool which will draw shapes (rectangles, triangles, stars…) when selected. I am guessing that I just need to be able to click drag draw a sprite which contains the shapes?

There’s an example of what I I’m talking about @ www.imagination3.com
If you go to Tools > Shapes you can see what I’m trying to emulate.

Any help will be appreciated.

Use Sprite.graphics.drawPolygon(). Then it’s just a little trig to figure out where the points should go.

Thanks for the reply Fidodo. Do you mean trig as in trigonometry and does anyone have any working examples? Thanks

Yeah the shapes in that example are premade shapes that are being scaled based on dragging the mouse. Trying to work out the code to create a star from scratch would probably make my hair fall out!

To do it with premade sprites you could just place them on the stage on mouse down (using eventlisteners), and then until mouseup tie the scaling of the sprite to the mouse position. On mouse up you can then lock the sprite in place, maybe writing it to a bitmap object so that flash doesn’t slow down when loads of shapes are created.

Naw, the trigonometry is actually very simple. I prefer that method for simple shapes but for anything with a design or texture, definitely use premade sprites. But for polygons doing a tiny bit of programing isn’t bad because you can have any polygon you want without needing to create new stamps, anything from 3 sides to infinity.

Heres a bit of code to show how simple it is:


temp.graphics.beginFill(0x0);
temp.graphics.moveTo(0,-radius);
for (var i:int = 1; i <= sides; i++) {
	var radian:Number = Math.PI*2*i/sides;
	temp.graphics.lineTo(-Math.sin(radian)*radius,-Math.cos(radian)*radius);
}
temp.graphics.endFill();

That’s for any polygon of “var sides” sides.

I don’t know why I said drawPolygon(). That function doesn’t exist :smiley:

Hey thanks for the code.

Someone has kindly supplied their source file for a drawing app. This might be useful for others too.

[quote=]I just created an actionscript 3 draw application, you can find the app and the source at my new blog:
http://www.urlvars.com/draw-app :rolleyes:[/quote]