Search code examples
iosobjective-cswiftsprite-kitskshapenode

SKShapeNode .strokeColor to look same as .fillColor when change node.alpha


When I change SKShapeNode's .alpha, its stroke shows itself as if .strokeColor of node was brighter than its .fillColor. Setting .lineWidth to 0 canceles antialiased smotheness that the stroke provides. Same does setting .strokeColor to SKColor.clearColor.

SKShapeNode stroke alpha issue

Question

What is the best way to make stroke look the same as fill when changing node.alpha, preserving the antialiased smootheness of edges?


Solution

  • Though it is possible to set alpha of node's stroke directly by getting its' colors' HSBA and reassigning a new color with different alpha to the stroke (for more info look this answer), as the node's fill covers node's stroke, stroke's alpha must be set to 0 so that it actually wouldn't be seen. But this canceles antialiasing.

    So it appears, that the only way to achieve smooth node's edges with no stroke be seen, is to convert SKShapeNode to SKSpriteNode and reset sprite.alpha to desired.

    smooth

    Here is a code snippet in Swift

    let sprite = SKSpriteNode(texture: self.view!.textureFromNode(shapeNode))
    

    Its' not that bad, because, according to Apple's docs

    The node being rendered does not need to appear in the view’s presented scene.

    However, you still have to have access to the view from where the conversion takes place.