Re-position scene to top of view

111 views Asked by At

Apple's documentation states I can't change the position using the position getter/setter. I've tried the anchor point suggestion but still unable to move my scene's position in Scene Editor to the top. Does anyone have any ideas?

Implementation has been done via Scene Editor, currently the only code submitted to help scene present is below:

import UIKit
import SpriteKit
import GameplayKit

class GameViewController: UIViewController {
 
    override func viewDidLoad() {
        super.viewDidLoad()
     
 
      // Load 'GameScene.sks' as a GKScene. This provides gameplay related content
        // including entities and graphs.
        if let scene = GKScene(fileNamed: "GameScene") {

            // Get the SKScene from the loaded GKScene
            if let sceneNode = scene.rootNode as! GameScene? {

                // Copy gameplay related content over to the scene
                sceneNode.entities = scene.entities
                sceneNode.graphs = scene.graphs

                // Set the scale mode to scale to fit the window
                sceneNode.scaleMode = .aspectFit

                print(sceneNode.anchorPoint)
                print(sceneNode.position)


                // Present the scene
                if let view = self.view as! SKView? {
        
                    view.presentScene(sceneNode)
                    view.ignoresSiblingOrder = true

                    view.showsFPS = true
                    view.showsNodeCount = true
                }
            }
        }
        
        
    }

enter image description here

1

There are 1 answers

0
Stefan Ovomate On BEST ANSWER

In AppDelegate bypass storyboard.

 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    
     window = UIWindow(frame: UIScreen.main.bounds)
     let viewController = GameViewController()
     window?.rootViewController = viewController
     window?.makeKeyAndVisible()

    return true
}

In GameViewController

 class GameViewController: UIViewController {


   override func viewDidLoad() {
      super.viewDidLoad()
    
      view.addSubview(skView)
      setupSKView()
    
  }


   let skView: SKView = {
      let view = SKView()
    
      if let scene = SKScene(fileNamed: "GameScene") {
        scene.scaleMode = .fill
        view.presentScene(scene)
      }
      return view
    }()

    func setupSKView(){
        skView.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height / 2)
    }
}