Tyxc
Tyxc

Reputation: 33

SpriteKit Swift Positioning HUD

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.

Upvotes: 0

Views: 560

Answers (1)

ABakerSmith
ABakerSmith

Reputation: 22939

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.

Upvotes: 1

Related Questions