好的,所以我一直试图在互联网上找到这个问题的答案,似乎我几乎得到了它。所以我所做的是我在一个类之外声明我的纹理atlases变量,使它们在我的整个项目中都是全局的。像这样:
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出现后,会弹出一个灰色的屏幕,然后它会带我进入游戏。但是玩家不再工作了!现在它无法移动或其他任何东西..
答案 0 :(得分: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游戏项目的一部分。如果纹理图集数组为空,则不会调用完成处理程序。)