0

I'm trying to position a simple black box to use as reference for my HUD elements for a game. But the positioning of it is always really off, mainly the y position of the box. I tried to position my hud box to be at the very top of the screen and to spawn the whole width, but the CGRectGetMaxY(screenSize) positions it at the very bottom of the screen.

var hudLayer = SKSpriteNode(color: SKColor.blackColor(), size: CGSizeMake(self.size.width, 200))
            hudLayer.anchorPoint = CGPointMake(0, 1)
            hudLayer.position = CGPointMake(CGRectGetMinX(screenSize), CGRectGetMaxY(screenSize))
            hudLayer.zPosition = 100
            self.addChild(hudLayer)

How did I manage to get my maxY coordinate to be at the very bottom? I'm perplexed and have tried other methods of y-positioning (self.size.height). So far, I find myself having to multiply the CGRectGetMaxY(screenSize) by 3.6 to get it to the top, but I'd rather not.

Any explanation as to why my y-coordinates are incredibly off? I set the anchor point, so it should be using the upper-left corner to position itself. Another issue is that I can't make this look the same on the 4s. On the 4s simulator, the hud bar appears much lower, despite using screenSize to calculate relative position.

Thanks.

Tyxc
  • 33
  • 6
  • Have a look at this, the question talks about iPad and iPhone but your problem could be the same. http://stackoverflow.com/questions/29451131/spritekit-coordinates-differ-between-ipad-and-iphone/29451330#29451330 – ABakerSmith Apr 22 '15 at 23:28
  • As others have said, it looks like your scene is being scaled. To fix it see here http://stackoverflow.com/a/25256339/2158465 . But if you want to keep your scene scaled the way it is you will need to position nodes relative to the view, see here http://stackoverflow.com/a/29171224/2158465 – Epic Byte Apr 23 '15 at 19:42
  • That second link is actually the most relevant to my issue since I also set the resolution at the start- except mine is in landscape view. Thanks! – Tyxc Apr 23 '15 at 20:42

1 Answers1

1

Your problem sounds like your SKScene's size isn't the same as your SKView's size. To fix this you should set your scene's size and scale mode:

scene.scaleMode = .AspectFill
scene.size = skView.bounds.size

The easiest place to set this is probably in your GameViewController, when you're setting up your SKScene. Alternatively though, you could set this in the didMoveToView method, in your SKScene.

ABakerSmith
  • 22,759
  • 9
  • 68
  • 78
  • Oh wow. That never occurred to me that that could be a problem, I always though it was automatically calculated. Thanks! I'll try this out now! – Tyxc Apr 23 '15 at 00:11
  • Yeah, the size of your scene will be whatever you've set it to in the scene's .sks file. Perhaps your problem will be something they fix in the future, but currently sprite-kit doesn't really support different screen sizes the way UIKit does. – ABakerSmith Apr 23 '15 at 00:23
  • I just changed it in my GameViewController and now everything is blown out of proportion. I'm going to see if I should just scale everything down in my code. Hope this works! – Tyxc Apr 23 '15 at 00:24
  • Did it work in the end? Were you doing anything do scale nodes up? – ABakerSmith Apr 23 '15 at 09:41
  • Not really. My code is set up like this guys's: http://stackoverflow.com/questions/29161881/skspritenode-position-in-universal-game/29171224#29171224 So, in the end, it's a lot more work to resize everything by setting the skView size. But now I know that that's a thing. :) – Tyxc Apr 23 '15 at 21:09
  • Do you need to run your app on different devices then? – ABakerSmith Apr 23 '15 at 21:12
  • Nope. I think I'm getting closer to the solution. I have to convert the positioning of my UI to somehow be relative to the playable area that's on screen. I've been testing it out on XCode's simulator's since I don't have all that many devices to actually test it on. – Tyxc Apr 23 '15 at 21:58