My issue is the HUD isn't staying in the same position but moving with the camera also.
I'm trying to create a version of Sokoban using swift_boxxle code found on Github,
I've added a new SKNode called mazeWorld, a camera node and a HUD node.
var mazeWorld: SKNode?
let cam = SKCameraNode()
var HUD: SKNode?
In my didMoveToView I'm adding both the mazeWorld and the HUD to the scene.
override func didMoveToView(view: SKView) {
HUD = SKNode()
addChild(HUD!)
mazeWorld = SKNode()
addChild(mazeWorld!)
self.camera = cam
...
initMenu()
}
I'm using the update function to set the camera position to the player position:
override func update(currentTime: CFTimeInterval) {
cam.position = player.sprite.position
}
I've got another function to setup the menu
func initMenu() {
menuReset.text = "[reset]"
menuReset.fontSize = 20
menuReset.fontColor = SKColor.whiteColor()
menuReset.position = CGPoint(x: self.size.width/2, y: self.size.height/2 - 20)
menuReset.horizontalAlignmentMode = SKLabelHorizontalAlignmentMode.Left;
...
HUD!.addChild(menuMain)
}
Update
I added the following:
let label = UILabel(frame: CGRectMake(0, 0, 200, 21))
label.textAlignment = NSTextAlignment.Center
label.text = "I'am a test label"
label.textColor = UIColor.whiteColor()
self.view!.addSubview(label)
With SO Answer
var frame: CGRect = label.frame
//align on top right
let xPosition: CGFloat = CGRectGetWidth(view.frame) - CGRectGetWidth(frame)
frame.origin = CGPointMake(ceil(xPosition), 0.0)
label.frame = frame
//autoresizing so it stays at top right (flexible left and flexible bottom margin)
view.autoresizingMask = [.FlexibleLeftMargin, .FlexibleBottomMargin]