How to draw a line in Sprite-kit

spritekit draw rectangle
skshapenode to skspritenode
skshapenode triangle
cgpath
spritekit vector graphics
skspritenode circle
skspritenode rounded corners swift
spritekit ios 13

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?

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)

How to Make a Line Drawing Game with Sprite Kit and Swift , In this Swift and Sprite Kit video tutorial, we'll create Swipe lines by populating an array of Duration: 13:44 Posted: Dec 12, 2017 In a line drawing game, you trace a line with your finger and then sprites follow the line that you drew. In this tutorial, you’ll learn how to write your own line drawing game with Sprite Kit. Instead of being a mere clone of Flight Control, you will create a game called “Hogville” where your goal is to bring some cute and tired pigs to food and shelter.

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];

Path and SKShapeNode Based Swipe Lines, Download our FREE CartoonSmart TV app http://bit.ly/cartoonsmart (for iOS and Apple TV Duration: 17:46 Posted: Dec 11, 2017 Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. Learn more Draw lines with Sprite kit and swift

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];

Sprite Based Swipe Lines - using SpriteKit and Swift, The length of the line defined by the shape node. Customizing Stroking or Fill Drawing. Take per-pixel control of drawing  Active5 years, 5 months ago. Viewed5k times. 5. 2. I want to draw a dashed line in my sprite kit game, I can use SKShapeNode node to draw a normal line like the following: UIBezierPath *_path=[UIBezierPath bezierPath]; //1 CGPoint point1 = CGPointMake(100,100); CGPoint point2 = CGPointMake(150,150); [_path moveToPoint:point1]; [_path addLineToPoint:point2]; //2 SKShapeNode *line = [SKShapeNode node]; line.path = _path.CGPath;

Swift 3 for drawing line via SKShapeNode:

            // Define start & end point for line
            let startPoint = CGPoint.zero
            let endPoint = CGPoint.zero

            // Create line with SKShapeNode
            let line = SKShapeNode()
            let path = UIBezierPath()
            path.move(to: startPoint)
            path.addLine(to: endPoint)
            line.path = path.cgPath
            line.strokeColor = UIColor.white
            line.lineWidth = 2

SKShapeNode, Screenshot of on the app window displaying an image (a sprite) using SpriteKit. Because the code in this article sets up a view, you put the lines from each of the​  The framework provides several tools to do that, such as many geometry functions and a fully-fledged physics engine. To draw sprites, the framework provides several nodes, including SKSpriteNode — the most used one. Nevertheless, in my case I didn’t want to use this feature because drawing a ruler demands drawing lines basically.

Here is the equivalent code in SWIFT:

    let pathToDraw:CGMutablePathRef = CGPathCreateMutable()
    let myLine:SKShapeNode = SKShapeNode(path:pathToDraw)

    CGPathMoveToPoint(pathToDraw, nil, 100.0, 100)
    CGPathAddLineToPoint(pathToDraw, nil, 50, 50)

    myLine.path = pathToDraw
    myLine.strokeColor = SKColor.redColor()

    self.addChild(myLine)

Converted from to @Rajneesh071's objective c code sample.

Drawing SpriteKit Content in a View, SpriteKit 22 To draw a line | In Codepad you can find +44000 free code snippets, HTML5, CSS3, and JS Demos. Collaborate with other web  I found this post while trying to draw a line on each mouseDown, of the example xCode / OS X / Game (aka SpriteKit)/ Application. You can copy/paste this code into GameScene.swift. It should draw a line on each mouse-down event by user.

SpriteKit 22 To draw a line, Drawing a ruler with SpriteKit - This spike was aimed at gathering a character can be a node (from a sprite), a text can be a node, a line can  comment tracer une ligne dans Sprite-kit? Par exemple, si je veux tracer une ligne dans cocos2d, je pourrais facilement utiliser ccDrawLine();. y a-t-il un équivalent dans sprite-kit?

Drawing a ruler with SpriteKit in Swift, When you create a new SpriteKit project, Apple puts some code into the files that you don't need, but is great for Next open GameViewController.swift and find this line: if let scene The above code is creating a custom SKSpriteNode class. This is how you can draw a dashed line in swift. You can change the parameters as you want. let bezierPath = UIBezierPath() let startPoint = CGPointMake(0,250) let endPoint = CGPointMake(450,250) bezierPath.moveToPoint(startPoint) bezierPath.addLineToPoint(endPoint) var pattern : [CGFloat] = [10.0,10.0]; let dashed = CGPathCreateCopyByDashingPath (bezierPath.CGPath, nil,0,pattern,2); var

Orange Tree with SpriteKit and Swift 4, lineDrawingSpriteKit. Paint / Line Drawing in Sprite Kit with Swift 3. Simulates a brush line that is removed when you finish drawing. Paints a line on the screen. Tag: ios,cocoa-touch,draw,sprite-kit For my program I need to take a line drawn using touches moved and SKShapeNode and give it collision properties. I looked at this thread: draw a line in sprite kit in touchesmoved where a line is drawn, but I am having trouble making it a physics body.

Comments
  • no correction at all, Waruna should give it a tick as its correct.
  • @Smick - Waruna will accept answer buddy whenever he want...dont worry.. its correct answer.. :)
  • can you try solve this - its a good question stackoverflow.com/questions/19214040/…
  • You should use SKColor, on iOS that compiles to UIColor, on OSX to NSColor. So it works on both platforms.
  • Is it possible to change the line's thickness ?