gpt4 book ai didi

swift - GameViewController 中的完成处理程序崩溃

转载 作者:可可西里 更新时间:2023-11-01 01:21:04 29 4
gpt4 key购买 nike

想在游戏开始前预加载 textureAtlases,所以我决定将场景启动代码放入完成处理程序中,但由于某些原因应用程序崩溃了。这是我的代码:

import UIKit
import SpriteKit
import GameplayKit

class GameViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()

let gpuAtlas = SKTextureAtlas(named: "GP")
let ppAtlas = SKTextureAtlas(named: "PP")

SKTextureAtlas.preloadTextureAtlases([gpuAtlas, ppAtlas]) {

if let view = self.view as? SKView {

let scene = GameScene(size: self.view.bounds.size)
// Set the scale mode to scale to fit the window
scene.scaleMode = .aspectFill

// Present the scene
view.presentScene(scene)


view.ignoresSiblingOrder = true

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

override var shouldAutorotate: Bool {
return true
}

override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return .portrait
}

override var prefersStatusBarHidden: Bool {
return true
}
}

如果我删除预加载纹理图集方法,一切都会照常工作,但我希望预加载纹理有一个变化,以摆脱由于在缓存中加载纹理而导致的第一个关键帧卡住。

错误:

fatal error: unexpectedly found nil while unwrapping an Optional value

更新:它在方法 didSimulatePhysics 中的 GameScene.swift 文件上崩溃。

伙计们,我做错了什么?

最佳答案

试试这个(使用 main thread 来操作 View 和 weak self):

SKTextureAtlas.preloadTextureAtlases([gpuAtlas, ppAtlas]) { [weak self] in
guard let gameView = self?.view as? SKView else { return }
DispatchQueue.main.async {
let scene = GameScene(size: gameView.bounds.size)
// Set the scale mode to scale to fit the window
scene.scaleMode = .aspectFill
// Present the scene
gameView.presentScene(scene)
gameView.ignoresSiblingOrder = true
gameView.showsFPS = true
gameView.showsNodeCount = true
}
}

你检查过 scene 是否是可选的吗?如果是这样:

guard let scene = GameScene(size: gameView.bounds.size) else { return }

关于swift - GameViewController 中的完成处理程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43896625/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com