I was trying to set the size of a SKSpriteNode
from a PNG image (OK with that, just a:
test = SKSpriteNode(imageNamed: "myImage")
But when it comes to resize it, I'm not sure if I should use test.setScale
or test.size
.
What's the difference and when should I use each of them?
setScale()
:
Sets the xScale and yScale properties of the node. These two parameters are the scaling factor that multiplies the width (for xScale) and the height (for yScale) of a node and its children.
Code sample:
spaceship.setScale(0.50) // decreased scale to it's half size
size()
:
Expresses the dimensions of the sprite (width and height), in points.
Code sample:
spaceship.size = CGSizeMake(100.0, 70.0) // Re-size the dimensions to these values
When do you use setScale
and when do you use size
?
Usually setScale
is used to increase or decrease the sprite about the scale factor, look for example the SKAction.scaleTo
used to make a zoom in or zoom out.
size
is frequently used because you can express in points the exactly values you want to do to your sprite.
You should use size when you want to change the specific size of a SKSpriteNode without affecting the size of any children of that sprite node.
Scaling a sprite node not only changes the size (scale) of the node, it also scales all that sprite node's children appropriately (proportionally) as well.
If a sprite node has no children, then there's no practical difference between size and scale.
Note: I find it helpful to use size when it doesn't matter so that I know that a scale of 1.0 is always the default size - handy for animating using scaleTo instead of scaleBy and back again to default size.
If you are using a spriteNode, I would use .setScale, or .xScale or .yScale , because those are specifically for sprites. .size can also be used for the size of labels, and text views in UIKit.
Lets say you want the sprite to be 2x bigger:
theNode.xScale = 2.0
theNode.yScale = 2.0
Note that they have to be floats, not Integers
So to sum it up, .setScale is for spriteNodes and images in general, and .size is for a vast amount of things that aren't images, like labels and more. So if you are using a sprite, use .setScale. If you are resizing a block use .size.
If you believe that this is the right answer, please mark it as so so it helps anyone else with this issue. Thanks!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With