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

雨燕双飞 提交于 2019-12-23 08:57:57

问题


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?


回答1:


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.




回答2:


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).



来源:https://stackoverflow.com/questions/20733953/why-is-the-coordinate-system-in-sprite-kit-flipped-and-can-i-change-that-globall

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!