将场景重新定位到视图顶部

问题描述

Apple 的文档说明我无法使用位置 getter/setter 更改位置。我已经尝试了锚点建议,但仍然无法将场景编辑器中的场景位置移动到顶部。有人有什么想法吗?

已通过场景编辑器实现,目前提交帮助场景的唯一代码如下:

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

解决方法

在 AppDelegate 中绕过故事板。

 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
}

在游戏视图控制器中

 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)
    }
}