Search code examples
iosswiftsprite-kitskshapenode

How to use paths to create a sprite


I am very new to SpriteKit, and unfortunately cannot find any resources online to find out how this works. Basically, I have tried to create a diamond (from other's questions and answers on here). This is what I have:

let node = SKShapeNode()

var path:CGMutablePath = CGPathCreateMutable()
CGPathMoveToPoint(path, nil, 200, 200)
CGPathAddLineToPoint(path, nil, 250, 250)
CGPathAddLineToPoint(path, nil, 200, 300)
CGPathAddLineToPoint(path, nil, 150, 250)
CGPathAddLineToPoint(path, nil, 200, 200)
CGPathCloseSubpath(path);
node.path = path;
node.fillColor = SKColor.orangeColor()
self.addChild(node)

I have placed this code in the viewDidLoad. However, when I run this, all I get is the grey screen... Can anyone assist me? And if so, is it possible to do the same for 2/3 of a circle instead of the diamond? I would like objects to be able to enter the cirlce... Can anyone also recommend anywhere where I can learn how to create 'paths' around objects?
I've tried using the bodyWithTexture:alphaThreshold:size: on the Apple guide, but cannot figure out how this works?
Sorry for such a long question, any help would be much appreciated!


Solution

  • Working with CGPath can be complicated sometimes, that's why people have built tools like the SKImport with an awesome editor, which creates you the objects and the CGPath which you can use in your project.

    enter image description here

    Link to original question