1

好的,所以我一直试图在整个互联网上找到答案,似乎我几乎得到了它。所以我所做的是我在一个类之外声明了我的纹理图集变量,以便它们在我的项目中是全局的。像这样的东西:

import SpriteKit
import CoreMotion

let myAtlas1 = SKTextureAtlas(named: "atlas1")
let myAtlas2 = SKTextureAtlas(named: "atlas2")
let myAtlas3 = SKTextureAtlas(named: "atlas3")

class GameScene: SKScene, SKPhysicsContactDelegate  {

然后,在我的 GameViewController 中,我正在预加载它们,完成后我将展示我的 GameScene,如下所示:

import UIKit
import SpriteKit

 class GameViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    SKTextureAtlas.preloadTextureAtlases([myAtlas1, myAtlas2, myAtlas3], withCompletionHandler: {() -> Void in
        NSLog("completed")
        self.startScene()
    })
}

func startScene() {

    let skView = self.view as SKView
    skView.showsFPS = true
    skView.showsNodeCount = true
    skView.showsPhysics = false
    skView.ignoresSiblingOrder = true
    let scene = GameScene(size: skView.bounds.size)
    scene.scaleMode = .AspectFit
    scene.size = skView.bounds.size
    skView.presentScene(scene)

}

当我运行游戏时,我的 LaunchImage 出现后,会弹出一个灰色屏幕,然后将我带到游戏中。但是播放器不再工作了!现在它无法移动或其他任何东西..

4

1 回答 1

0

在你的GameViewController而不是GameScene立即开始,在完成处理程序中进行。

例如:在 Xcode 中为 SpriteKit 创建一个新的游戏项目。在GameViewController.swift改变这样的viewDidLoad功能:

class GameViewController: UIViewController {
  override func viewDidLoad() {
    super.viewDidLoad()

    // This is not part of the newly created project:
    var tas = [SKTextureAtlas]()
    tas.append(SKTextureAtlas(named: "Ship"))

    // Call the preload and in the completion handler load and start the GameScene:
    SKTextureAtlas.preloadTextureAtlases(tas, withCompletionHandler: { () -> Void in
        NSLog("completed")
        self.startScene()
    })
  }

  // Put the GameScene loading code from viewDidLoad into its own function:
  func startScene() {
    if let scene = GameScene.unarchiveFromFile("GameScene") as? GameScene {
        // Configure the view.
        let skView = self.view as SKView
        skView.showsFPS = true
        skView.showsNodeCount = true

        /* Sprite Kit applies additional optimizations to improve rendering performance */
        skView.ignoresSiblingOrder = true

        /* Set the scale mode to scale to fit the window */
        scene.scaleMode = .AspectFill

        skView.presentScene(scene)
    }
  }
  ...
}

(请注意,此代码引用了“Ship.atlas”目录,该目录需要创建并且不是标准 Xcode SpriteKit 游戏项目的一部分。此外,如果纹理图集数组为空,则不会调用完成处理程序。)

于 2015-05-07T20:20:49.080 回答