1

Apple 的文档指出我无法使用位置获取器/设置器更改位置。我已经尝试了锚点建议,但仍然无法将场景编辑器中的场景位置移动到顶部。有没有人有任何想法?

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

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

在此处输入图像描述

4

1 回答 1

1

在 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)
    }
}
于 2021-02-23T12:53:16.053 回答