2

I'm trying to draw an SpriteKit button in SceneKit.

Seems to do everything right, but the objects go off the screen.

Code:

import UIKit
import QuartzCore
import SceneKit
import SpriteKit

class GameViewController: UIViewController , SCNSceneRendererDelegate {

var walkAnimButton: SKSpriteNode!
var overlay: SKScene!

override func viewDidLoad() {
    super.viewDidLoad()

...
    self.setupHUD()
}


func setupHUD () {

    let screenSize: CGSize = UIScreen.main.bounds.size
    let sceneView = self.view as! SCNView

    /* Create overlay SKScene for 3D scene */
    overlay = SKScene.init(size: sceneView.bounds.size)
    overlay.scaleMode = SKSceneScaleMode.aspectFill
    sceneView.overlaySKScene = overlay

    /* Create button for controlling walk animation */
    let walkAnimButton: SKSpriteNode = SKSpriteNode.init(imageNamed: "walking")
    walkAnimButton.anchorPoint = CGPoint(x: 0.5, y: 0.5)
    walkAnimButton.size = CGSize(width: 32, height: 32)
    walkAnimButton.position = CGPoint(x: screenSize.width-walkAnimButton.size.width, y: screenSize.height-walkAnimButton.size.height)
    walkAnimButton.name = "WalkAnimationButton"
    overlay.addChild(walkAnimButton)
}
}

What could be the error?

overlay.scaleMode = SKSceneScaleMode.aspectFill

Thanks in advance.

Oleg Savelyev
  • 275
  • 4
  • 16
  • You are mixing coordinate systems here, screen size and scene view size can be very different. I would not recommend doing `overlay = SKScene.init(size: sceneView.bounds.size)` if you plan on doing `aspectFill`. You should only be using this constructor with a constant size, not a variable size (Also, you do not need to use init, `overlay = SKScene(size: sceneView.bounds.size)` is the valid way to go) – Knight0fDragon Apr 10 '17 at 13:57
  • So if you want to keep your items pixel perfect, you use `SKScene(size:CGSize.zero)` with `overlay.scaleMode = .resizeFill`, if you want to keep items the same size proportionally to the screen, you pick an arbitrary size you want to work with, like `SKScene(size:CGSize(width:375,height:750))` with `overlay.scaleMode = .aspectFill` – Knight0fDragon Apr 10 '17 at 13:57
  • Thanks a lot Knight0fDragon ! It helps :-) – Oleg Savelyev Apr 10 '17 at 14:10

0 Answers0