Coordinate System in SpriteKit

痞子三分冷 提交于 2019-12-06 05:06:51

Ok so it is going to be tough to answer this because it isn't like you have one question but I will do my best to explain why things are happening the way they are.

First thing to think about. Your Scene is 1024 x 768 (guessing that because of one of your comments and because of your problem) Now this causes a few issues. First being your phone isn't that aspect ratio second your phone isn't that size. If you look at your ViewController you will likely see some AspectFill or AspectFit being set. This scales down your scene. Things are no longer 1:1 for positioning when it comes to what you see and what you do in code.

Lets go further. The Scene's origin is in the bottom left and depending on your scene size and scaling it may be off screen. This is where 0,0 is. So every child you add will start there and work its way right and up based on position. A SKSpriteNode has its origin in the center. Your background for example would have an origin in the center. Notice how you set the position to half the frames width and height. Its origin is in the middle and you moved it over to the right and up the correct distance form the bottom left. Now when you add a child node to that node it starts from the origin of its parent. In this example the center. So that is why you have to give it negative values to get down to the bottom left when you add a ball to the background.

As far as nodes not showing up. If you look in your ViewController you will likely see ignoresSibling order or something to that nature. This means you have to set the zPosition of your nodes. Lower zPositions get rendered first so background is usually 0 and all nodes to show above background get a zPosition of 1 or higher.

Hopefully that helps. Although your questions are well documented it makes it very difficult to answer all your questions in one answer. In the future I would recommend breaking your questions up and asking a new question after the first one gets answered because you may learn the answer before you even have a chance to ask it =)

Case 1: No idea why it's 38-odd points off; I need to see the code that adds the nodes.

Cases 2 and 3: The scene and the sprite node have different anchor points. For SKScene it's (0.0, 0.0) and for SKSpriteNode it's (0.5, 0.5).

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