sprite-kitskshapenodesktexture

SKShapeNode filltexture() does not display image


I want to create a circle that whose content is an image (.png), and based on the SKShapeNode class reference, I thought that I could use SKShapeNode.filltexture() function to set the texture to the image. But when i run the code below, I get the circle, but the image of the "cat-black" I am trying to load doesn't show. I checked that my Image.Assets have the image with the correct name, so something else is up. Any ideas? I attached the output below:

 func newCircle(photo:String, position:CGPoint) -> SKShapeNode {

    let circle = SKShapeNode.init(circleOfRadius: 27)
    circle.fillTexture = SKTexture.init(image: UIImage(named: "cat-black")!)
    circle.strokeColor = UIColor.greenColor()
    circle.lineWidth = 4
    circle.name = "aggroNode"
    circle.position = position
    return circle

}

enter image description here


Solution

  • The documentation states

    The default value is nil. If a fill texture is specified, the fillColor property is ignored and the filled portion of the shape node is rendered using the texture instead [emphasis added].

    This suggests that the shape must be filled or the texture will not be visible; therefore, if you set the shape's fill color with

    circle.fillColor = .white
    

    the texture will appear. You can also set fillColor with a different color to colorize the texture:

    circle.fillColor = .blue