What is wrong with the coordinate system in this SpriteKit setup?

I created an SKView that represents a subclass of SKScene as follows:

SKView *skv = [[SKView alloc] initWithFrame:self.view.bounds]; [self.view addSubview:skv]; self.skScene = [[TestScene alloc] initWithSize:v.bounds.size]; [skv presentScene:_skScene]; 

Then, to see the origin, I add a small 10x10 square to the scene.

 SKSpriteNode *ori = [SKSpriteNode spriteNodeWithColor:[UIColor greenColor] size:CGSizeMake(10,10)]; [self addChild:ori]; 

You can see the green square in the lower left corner:

green dot in lower left corner

From what I thought, SpriteKit's coordinate system is such that the origin is always in the center. But on the stage, the source is in the lower left corner. When I add a node child to {0,0}, it also appears in the lower left corner.

When I add SKSpriteNode and position it {0,0} in the scene, it appears in the lower left corner. But it is centered around the beginning of the scene in the lower left corner (cut off from half to the left and half to the bottom).

But now it is becoming more confusing. When I add SKSpriteNode to another SKSpriteNode, the sub-sprite is CENTERED in the parent.

Does this mean that the coordinate system of the scene does not work like the coordinate system of the sprite?

Summary:

  • When I place the sprite in the scene at {0,0}, it appears at the bottom left, cropped by 50% (centered around the source).
  • When I put a sprite in a sprite at {0,0}, it appears in the center of the sprite.

Is my scene set up incorrectly or is it the way it works?

+8
ios ios7 sprite-kit skspritenode sknode
source share
3 answers

And so it works. All OpenGL views (at least 2D) have a coordinate origin in the lower left corner.

The position of your sprites is also correct. Both are located by default at 0.0. The sprite texture is drawn relative to the node position based on the anchorPoint coefficient. The default value is 0.5, 0.5 places the texture at the node position.

You can change the anchor point of the scene to 0,5,0,5, which moves the sprites to the center of the scene. You can also change the sprite's anchor, but this is not recommended, as it affects things like rotation, scale, collision detection, and the position of the child node.

+15
source share

you can add this code to GameViewController.swift:

 override func viewDidLoad() { super.viewDidLoad() // Detect the screensize var sizeRect = UIScreen.mainScreen().applicationFrame var width = sizeRect.size.width * UIScreen.mainScreen().scale var height = sizeRect.size.height * UIScreen.mainScreen().scale // Scene should be shown in fullscreen mode let scene = GameScene(size: CGSizeMake(width, height)) // Configure the view. let skView = self.view as! SKView skView.showsFPS = true skView.showsNodeCount = true /* Sprite Kit applies additional optimizations to improve rendering performance */ skView.ignoresSiblingOrder = true /* Set the scale mode to scale to fit the window */ scene.scaleMode = .AspectFill skView.presentScene(scene) } 
+3
source share

What I did was set the size of the SKScene that you want to show on bounds.size in the Viewcontroller from which you want to show the scene. Something like that:

  if let scene = GameScene(fileNamed:"GameScene") { // Configure the view. let skView = self.view as! SKView skView.showsFPS = true skView.showsNodeCount = true /* Sprite Kit applies additional optimizations to improve rendering performance */ skView.ignoresSiblingOrder = true scene.size = skView.bounds.size /* Set the scale mode to scale to fit the window */ scene.scaleMode = .AspectFill skView.presentScene(scene) } 

I hope this worked for you too, if I made any mistakes here, please let me know :)

0
source share

All Articles