How to create Xcode SWIFT overlay to display HUD or controls?
I have a scene created with SceneKit. This is a game that uses SceneKit. I am trying to make a HUD so I can set the controls on the screen using SpriteKit. The problem is that I have no code at all and I don't know what I am missing. Can you rewrite it correctly? I tried to add an on-screen sprite image called base, but I'm not sure if you add a child after you have overlay SpriteKit overlaySKScene?
code:
import iAd
import UIKit
import GameKit
import SceneKit
import StoreKit
import SpriteKit
import QuartzCore
import Foundation
import AVFoundation
import AudioToolbox
//============================================================
class GameViewController: UIViewController, ADBannerViewDelegate, SKPhysicsContactDelegate, SKSceneDelegate, SCNSceneRendererDelegate, SCNPhysicsContactDelegate{
let base = SKSpriteNode(imageNamed:"VirtualJoystickBase")
override func viewDidLoad() {
super.viewDidLoad()
let scnView = self.view as! SCNView
let skScene = scnView.overlaySKScene
scnView.overlaySKScene = skScene
scnView.backgroundColor = UIColor.whiteColor()
scnView.scene = FieldScene
scnView.delegate = self
scnView.allowsCameraControl = true
scnView.showsStatistics = false
base.size = CGSize(width: 100, height: 100)
base.anchorPoint = CGPointMake(-3.4, -5.2)
scnView.overlaySKScene?.addChild(base)
The main problem is with the following lines of code:
let skScene = scnView.overlaySKScene
scnView.overlaySKScene = skScene
These two lines are a place of completely useless action. By default it overlaySKScene
returns nil
for its own scnView
, because it doesn't have SKScene. And you store this object nil
before skScene
and then return it to scnView.overlaySKScene
(even if it wasn't, it nil
's absolutely useless to do that)
Then when you try to do this:
scnView.overlaySKScene?.addChild(base)
addChild
did not cause due scnView.overlaySKScene
nil
. This is why you have nothing at the end.
To fix this, you need to create an object skScene
, for example like this:
let overlayScene = SKScene(size: CGSizeMake(100, 100))
Then you can set position
for your object base
like this:
base.position = CGPointMake(50, 0)
Then add the object base
to the scene:
overlayScene.addChild(base)
Finally, set the scene like this:
scnView.overlaySKScene = overlayScene