简体   繁体   English

SKShapeNode行中出现白线

[英]White lines appearing in SKShapeNode line

I'm drawing a line using SKShapeNodes. 我正在使用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. 当用户点击屏幕时,该线应以45度角左右改变方向。 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 我正在使用以下代码创建该行:每0.1秒调用一次此函数

    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: 然后在update()函数中:

    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: 并且createPathToMove看起来像这样:

    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). 我在touchesBegan上更改了currentPosition变量(我只是使其与当前变量相反)。 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. 感谢您的帮助,我将很高兴采用客观的c代码并将其转换为快速的代码。

I fixed this myself by setting antialiasing to false. 我自己将抗锯齿设置为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. 尽管作为一个友好的说明,万一其他人遇到此问题,即使将抗锯齿设置为false,随机行仍然会出现在模拟器上,但是在实际设备上运行时,它仍然可以正常工作。 I'm guessing it is just a bug in how the simulator is rendering the shape. 我猜这只是模拟器如何渲染形状的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM