Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the coordinate system in sprite kit flipped and can I change that globally?

I noticed that in Sprite Kit the coordinate system is flipped.

For example, here is a SKSpriteNode:

SKSpriteNode *car = [SKSpriteNode spriteNodeWithImageNamed:@"car"];
car.position = CGPointMake(0, 0);
[road addChild:car];

The car is positioned in the center of it's parent.

When I set position to:

car.position = CGPointMake(-50, 0);

then it is positioned more to the left. But when I want to move it down, and increase Y, it moves UP!

car.position = CGPointMake(-50, 20);

In UIKit increasing Y always means something moves down. It feels more logical to me. Is there a way to flip the coordinate system in Sprite Kit?

like image 273
openfrog Avatar asked Dec 22 '13 20:12

openfrog


2 Answers

You can set your scene's yScale property to -1. Everything will render upside down, but you can also set the yScale property for each node to -1, which will cause them to render right side up.

You could also make a class category and create a property called invertedPosition with a custom getter/setter that inverts the default position property.

like image 181
godel9 Avatar answered Nov 02 '22 21:11

godel9


Sprite Kit uses a coordinate orientation that starts from the bottom left corner of the scene (0,0), and the values increase as you move to the right (increasing x) and up (increasing y).

like image 31
alpennec Avatar answered Nov 02 '22 21:11

alpennec