It is simple. I set my sprite's centerRect as described in Apple Documentation but the image it displays gets distorted (as I hadn't defined centerRect property). My code:
let sprite = SKSpriteNode()
sprite.texture = SKTexture(imageNamed: "ImageName")
sprite.centerRect = CGRect(x: 0.49, y: 0.49, width: 0.02, height: 0.02)
sprite.scale(to:CGSize(width: myCustomWidth, height: myCustomHeight))
//sprite.size = CGSize(width: myCustomWidth, height: myCustomHeight)
I don't know where did I make a mistake or whether it is something missing in my code.
Thank you in advance.
I finally found the solution to the problem. Actually it was about how scale(to:) method and size property works.
My problem was about how myCustomWidth and myCustomHeight were defined from the size property. These values belong to interval ((0,0),(0,0)) to ((1,1),(1,1)) so I was telling the computer to shrink the image. Therefore the centerRect property could not be applied since the image wasn’t enlarged. So with values greater than image size in scale function and setting the actual size property to get the desired size on screen the problem was solved.
Now I’ll explain it in depth:
The size property changes your sprite’s size distorting your image if you don’t keep the same proportion between height and width.
The scale(to:) method uses the centerRect property to scale the node. If the result after the scale being applied is greater than the original image, it will use centerRect to scale without distorting the corners. Otherwise it could change sprite size without applying centerRect at all!
If your image is greater than the space you want the sprite to cover you can apply scale first and then resize (size property) preserving the proportion.
For any doubts, (or corrections) I would be pleased to help you.