frankibanki frankibanki - 3 months ago 29
Swift Question

Swift SpriteKit creating mutable path (CGMutablePathRef)

I found this code on stack overflow.

CGMutablePathRef path = CGPathCreateMutable()
CGPathMoveToPoint(path, nil, 0, -(self.tileSize.height / 2))
CGPathAddLineToPoint(path, nil, (self.tileSize.width / 2), 0)
CGPathAddLineToPoint(path, nil, 0, (self.tileSize.height / 2))
CGPathAddLineToPoint(path, nil, -(self.tileSize.width / 2), 0)
CGPathCloseSubpath(path)


its a part of an answer and the code should create a path that have the shape of an isometric tile but its not working. So somebody knows why and how to create this mutablePath?
Thank you.

Answer

If your problem is that the code does not compile, you just need to convert it from Objective-C to Swift

let path = CGPathCreateMutable()
CGPathMoveToPoint(path, nil, 0, -(self.tileSize.height / 2))
CGPathAddLineToPoint(path, nil, (self.tileSize.width / 2), 0)
CGPathAddLineToPoint(path, nil, 0, (self.tileSize.height / 2))
CGPathAddLineToPoint(path, nil, -(self.tileSize.width / 2), 0)
CGPathCloseSubpath(path)