Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to draw a line in Sprite-kit

How can one draw a line in Sprite-kit? For example if I want to draw a line in cocos2d, I could easily using ccDrawLine();

Is there an equivalent in sprite-kit?

like image 651
Waruna Avatar asked Sep 30 '13 10:09

Waruna


3 Answers

Using SKShapeNode you can draw line or any shape.

SKShapeNode *yourline = [SKShapeNode node];
CGMutablePathRef pathToDraw = CGPathCreateMutable();
CGPathMoveToPoint(pathToDraw, NULL, 100.0, 100.0);
CGPathAddLineToPoint(pathToDraw, NULL, 50.0, 50.0);
yourline.path = pathToDraw;
[yourline setStrokeColor:[SKColor redColor]];
[self addChild:yourline];

Equivalent for Swift 4:

var yourline = SKShapeNode()
var pathToDraw = CGMutablePath()
pathToDraw.move(to: CGPoint(x: 100.0, y: 100.0))
pathToDraw.addLine(to: CGPoint(x: 50.0, y: 50.0))
yourline.path = pathToDraw
yourline.strokeColor = SKColor.red
addChild(yourline)
like image 149
Rajneesh071 Avatar answered Nov 20 '22 03:11

Rajneesh071


Using SKShapeNode I was able to do this.

// enter code here
SKShapeNode *line = [SKShapeNode node];

CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, 50.0, 40.0);
CGPathAddLineToPoint(path, NULL, 120.0, 400.0);

line.path = path;
[line setStrokeColor:[UIColor whiteColor]];

[self addChild:line];
like image 34
Waruna Avatar answered Nov 20 '22 02:11

Waruna


If you only want a line, sort of how people use UIViews for lines (only), then you can just use a SKSpriteNode

SKSpriteNode* line = [SKSpriteNode spriteNodeWithColor:[SKColor blackColor] size:CGSizeMake(160.0, 2.0)];
[line setPosition:CGPointMake(136.0, 50.0))];
[self addChild:line];
like image 8
John Riselvato Avatar answered Nov 20 '22 03:11

John Riselvato