Hey guys,
Introducing the RDDrawingSprite class! It draws stuff, it mows your lawn, it makes you breakfast.... well, not really. It just allows drawing.
It supports drawing lines, rectangles, and circles. It can even draw lines between an array of CGPoints.
There is no support for triangles, but that's because they can easily be created manually.
Each shape or drawing can have its own style, which includes a line thickness, line color, fill color and alpha (0-100%). Note that a lack of a fill color will mean the drawing will not loop (aka last point drawn connects to first point; see example 4).
Enjoy! http://filebeam.com/9bf43181f57f97132455f68282fab437
Here's an example of how it can be used (this example as an xcode project is included with the class above):
// Create a new Drawing Sprite
RDDrawingSprite *mds = [RDDrawingSprite createDrawingSprite];
[self addChild:mds];
// Create a few Styles to use
RDDrawingSpriteStyle *style1 = [RDDrawingSpriteStyle styleWithThickness:2 lineColor:ccRED alpha:77 fillColor:ccYELLOW];
RDDrawingSpriteStyle *style2 = [RDDrawingSpriteStyle styleWithThickness:3 lineColor:ccYELLOW alpha:90];
RDDrawingSpriteStyle *style3 = [RDDrawingSpriteStyle styleWithThickness:4 lineColor:ccMAGENTA alpha:100 fillColor:ccWHITE];
RDDrawingSpriteStyle *style4 = [RDDrawingSpriteStyle styleWithThickness:5 lineColor:ccBLUE alpha:85];
// Example 0
// Draw something that gets cleared
[mds drawLineFrom:ccp(0,0) to:ccp(100,100) withStyle:style2];
[mds clear];
// Example 1
// Set style for drawing fun things
// -- This style is used for manual drawing via startDrawing and endDrawing ,
// as well as draw methods where a style is not provided via withStyle .
[mds setStyle:style1];
// Example 2
// Draw something random
[mds startDrawing];
[mds moveTo:ccp(10 , 10)];
[mds lineTo:ccp(110 , 10)];
[mds lineTo:ccp(110 , 110)];
[mds endDrawing];
// Example 3
// Draw a line from point A to B with a specific style
[mds drawLineFrom:ccp(460,arc4random()%200) to:ccp(440,300) withStyle:style2];
// Example 4
// Meh, draw something else
// -- note that it doesn't connect like the first random shape did.
// This is because there is no fillColor selected for style4, which means
// Fill is disabled. Useful for drawing paths.
[mds setStyle:style4];
[mds startDrawing];
[mds moveTo:ccp(120 , 10)];
[mds lineTo:ccp(220 , 10)];
[mds lineTo:ccp(175 , 100)];
[mds lineTo:ccp(150 , 75)];
[mds lineTo:ccp(160 , 50)];
[mds endDrawing];
// Example 5
// Ellipse with width/height, center and a style
[mds drawEllipse:100 height:50 center:ccp(150,160) withStyle:style3];
// Example 6
// Circle with a radius, center. No style is provided, so the default style set above is used.
[mds drawCircle:50 center:ccp(290,90)];
// Example 7
// Drawing a line based on a set of points stored in an NSArray as NSValue entries.
// -- To make live easier, ccpasnsv has been defined as a macro in the header file.
// This macro creates a CGPoint and stores it in a new NSValue .
NSArray *arr = [NSArray arrayWithObjects:
ccpasnsv(100,200),
ccpasnsv(150,250),
ccpasnsv(200,200),
ccpasnsv(250,250),
ccpasnsv(300,200),
nil
];
[mds drawLineWithArray:arr withStyle:style4];
// Example 8
// Drawing a line based on a set of points stored in a C-style array.
// -- While not very Objective-C, it's useful for those scared of dealing with storing structs
// in an Objective-C NSArray
CGPoint array[] = {ccp(400,300), ccp(375,250), ccp(400, 200), ccp(375, 150), ccp(400, 100), ccp(375, 50)};
[mds drawLineWithCArray:array count:sizeof(array)/sizeof(CGPoint) withStyle:style4];
// Example 9
// Draw a rectangle from a CGRect with a style.
[mds drawRectangle:CGRectMake(15, 230, 100, 75) withStyle:style3];
Cheers,
-robodude666