Trying to code my first sprite kit game. At the top of the screen I will have a menu area, that will have buttons, game time, etc. The game's skscene
will be positioned directly below. Both will be visible at the same time.
Since the menu is just a menu, my idea is to make that area a UIView
with ordinary UIButtons
, i.e. no sprite kit nodes. Then, below the menu, add an SKView
, have it fill the remainder of the screen and present the game scene. So the menu area is a UIView
, and the game area is a sibling SKView
.
Is this best practice? From many of the examples I come across, it feels like sprite kit intends you to have just one SKView, and present the scene full screen, and include any menu buttons inside the scene itself as sprite nodes. But I don't see the advantage in doing it like that.
In many games, the "main menu" you describe would be called an "HUD," or Heads Up Display. It shows all the buttons that you require and is simply a SKSpriteNode at the top of your screen. You can add SKSpriteNodes as child nodes of the HUD that, when tapped, will perform an action:
let button = SKSpriteNode()
button.position = CGPointMake((-200), 0)
button.color = SKColor.redColor()
button.size = CGSizeMake(15, 15)
button.name = "button"
HUD.addChild(button)
Check if that button was pressed:
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
for touch in (touches as! Set<UITouch>) {
let location = touch.locationInNode(self)
let node = nodeAtPoint(location)
if node.name = "button" {
//run code
}
}
}
This method is fairly easy, but you have to give up UIButtons. You also will probably want to design the nodes you will use as buttons: I used this editor.
Hope this helped and good luck.