扩展具有固定轮廓宽度的圆

时间:2014-07-21 15:05:10

标签: ios swift sprite-kit skaction skshapenode

我想要的效果就像水上的雨滴,波从中心点向外辐射。

我创建了一个圆形:

    let initialRadius = 4.0
    let myPath: CGMutablePathRef = CGPathCreateMutable()
    CGPathAddArc(myPath, nil, 0, 0, initialRadius, 0, M_PI * 2, true)

    let myCircle = SKShapeNode()
    myCircle.path = myPath
    myCircle.position = CGPoint(x: xPos, y: yPos)
    myCircle.lineWidth = 0.5
    myCircle.antialiased = false
    myCircle.fillColor = SKColor.orangeColor() // background is orange
    myCircle.strokeColor = SKColor.whiteColor()
    myCircle.physicsBody = SKPhysicsBody(edgeLoopFromPath: myPath)

然后让sprite扩展,淡出:

    let duration: NSTimeInterval = 1.7
    let fade = SKAction.fadeOutWithDuration(duration)
    let scale = SKAction.scaleTo(9.0, duration: duration)
    fade.timingMode = .EaseOut
    scale.timingMode = .EaseOut
    myCircle.runAction(fade)
    myCircle.runAction(scale)

这很接近,但是当圆圈的大小正确扩展时,lineWidth也在扩展。你能想到一种方法吗,同时保持线宽不变吗?

1 个答案:

答案 0 :(得分:1)

您可以使用customActionWithDuration以增加半径(和恒定线宽)重新绘制路径,而不是使用scale。类似的东西:

    // ... All as before, except...    
    // myCircle.runAction(scale)
    typealias ActionBlock = ((SKNode!, CGFloat) -> Void)
    let ab: ActionBlock = { (node, value) in
        if let drop = node as? SKShapeNode {
            let myPath: CGMutablePathRef = CGPathCreateMutable()
            CGPathAddArc(myPath, nil, 0, 0, initialRadius * (1.0 + value * 9.0 / duration) , 0, M_PI * 2, true)
            drop.path = myPath
        }
    }

    let newScale = SKAction.customActionWithDuration(duration, actionBlock: ab)
    myCircle.runAction(newScale)