I'm drawing a line using SKShapeNodes. This line is constantly "growing", or always moving up. When the user taps on the screen the line should change directions, right or left, at a 45 degree angle. It does this, however for some reason random white lines appear in the line when it changes directions. They look like this:
Those white lines shouldn't be there, and I have no idea what's causing them. I'm creating the line with the following code: Every 0.1 seconds, this function is called
func addPoint() {
if (wayPoints.isEmpty) {
wayPoints.append(CGPointMake(0, CGRectGetMinY(self.frame)))
} else {
if (currentPosition == Position.Right) {
wayPoints.append(CGPointMake(wayPoints.last!.x + 1, wayPoints.last!.y + 1))
} else if (currentPosition == Position.Left) {
wayPoints.append(CGPointMake(wayPoints.last!.x - 1, wayPoints.last!.y + 1))
} else {
wayPoints.append(CGPointMake(0, wayPoints.last!.y + 1))
}
}
}
And then in the update() function this:
func drawLines() {
enumerateChildNodesWithName("line", usingBlock: {node, stop in
node.removeFromParent()
})
if let path = createPathToMove() {
let shapeNode = SKShapeNode()
shapeNode.path = path
shapeNode.name = "line"
shapeNode.strokeColor = UIColor.blackColor()
shapeNode.lineWidth = 20
shapeNode.zPosition = 5
shapeNode.lineCap = kCGLineCapRound
self.addChild(shapeNode)
}
}
And the createPathToMove looks like this:
func createPathToMove() -> CGPathRef? {
if wayPoints.count <= 1 {
return nil
}
var ref = CGPathCreateMutable()
for var i = 0; i < wayPoints.count; i++ {
let p = wayPoints[i]
if i == 0 {
CGPathMoveToPoint(ref, nil, p.x, p.y)
} else {
CGPathAddLineToPoint(ref, nil, p.x, p.y)
}
}
return ref
}
I change the currentPosition variable on touchesBegan (I just make it the opposite of what it currently is). I've looked around for a while, and haven't found anything similar to my problem. Is there something special I have to be doing to make sure these white lines don't appear? Any help is appreciated, and I will happily take objective c code and convert it to swift myself.
I fixed this myself by setting antialiasing to false. Although as a friendly note, incase anyone else runs into this problem, the random lines still occur on the simulator even with the antialiasing set to false, however when ran on a actual device it works properly. I'm guessing it is just a bug in how the simulator is rendering the shape.