Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference of setScale and size of SKSpriteNodes in SpriteKit and Swift?

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?

like image 869
J Curti Avatar asked Jul 29 '16 14:07

J Curti


3 Answers

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.

like image 191
Alessandro Ornano Avatar answered Nov 10 '22 22:11

Alessandro Ornano


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.

like image 7
Eric Avatar answered Nov 10 '22 23:11

Eric


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!

like image 3
Matthew Bergwall Avatar answered Nov 10 '22 21:11

Matthew Bergwall