gpt4 book ai didi

swift - 模拟器工作但再次呈现 UiViewController 时设备崩溃

转载 作者:搜寻专家 更新时间:2023-11-01 05:36:10 24 4
gpt4 key购买 nike

我一直在开发一款具有不同关卡的游戏,您可以通过菜单访问这些关卡。关卡是 3D 的并使用 SceneKit,菜单是 2D 的并使用 SpriteKit。

游戏从第 1 级开始,这是一个 UiViewController,当你击败它时,菜单就会出现。如果你在菜单中按下一个按钮(上面写着“Level 1”),你将被引导回到 level 1,但是由于某种原因在 iPhone 上再次加载 level 1 时,它崩溃了,但模拟器工作正常。

我这里有一个简单的示例代码,它做的事情几乎相同,并且只在设备上崩溃:

import UIKit
import QuartzCore
import SceneKit

class GameViewController: UIViewController {


func ajrj() {
presentViewController(GameViewController(), animated: true, completion: nil)
}


override func viewDidLoad() {
super.viewDidLoad()
self.view = SCNView()
var jarrarar = NSTimer.scheduledTimerWithTimeInterval(2, target: self, selector: #selector(GameViewController.ajrj), userInfo: nil, repeats: true)
// create a new scene
let scene = SCNScene(named: "art.scnassets/ship.scn")!

// create and add a camera to the scene
let cameraNode = SCNNode()
cameraNode.camera = SCNCamera()
scene.rootNode.addChildNode(cameraNode)

// place the camera
cameraNode.position = SCNVector3(x: 0, y: 0, z: 15)

// create and add a light to the scene
let lightNode = SCNNode()
lightNode.light = SCNLight()
lightNode.light!.type = SCNLightTypeOmni
lightNode.position = SCNVector3(x: 0, y: 10, z: 10)
scene.rootNode.addChildNode(lightNode)

// create and add an ambient light to the scene
let ambientLightNode = SCNNode()
ambientLightNode.light = SCNLight()
ambientLightNode.light!.type = SCNLightTypeAmbient
ambientLightNode.light!.color = UIColor.darkGrayColor()
scene.rootNode.addChildNode(ambientLightNode)

// retrieve the ship node
let ship = scene.rootNode.childNodeWithName("ship", recursively: true)!

// animate the 3d object
ship.runAction(SCNAction.repeatActionForever(SCNAction.rotateByX(0, y: 2, z: 0, duration: 1)))

// retrieve the SCNView
let scnView = self.view as! SCNView

// set the scene to the view
scnView.scene = scene

// allows the user to manipulate the camera
scnView.allowsCameraControl = true

// show statistics such as fps and timing information
scnView.showsStatistics = true

// configure the view
scnView.backgroundColor = UIColor.blackColor()

// add a tap gesture recognizer
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTap(_:)))
scnView.addGestureRecognizer(tapGesture)
}

func handleTap(gestureRecognize: UIGestureRecognizer) {
// retrieve the SCNView
let scnView = self.view as! SCNView

// check what nodes are tapped
let p = gestureRecognize.locationInView(scnView)
let hitResults = scnView.hitTest(p, options: nil)
// check that we clicked on at least one object
if hitResults.count > 0 {
// retrieved the first clicked object
let result: AnyObject! = hitResults[0]

// get its material
let material = result.node!.geometry!.firstMaterial!

// highlight it
SCNTransaction.begin()
SCNTransaction.setAnimationDuration(0.5)

// on completion - unhighlight
SCNTransaction.setCompletionBlock {
SCNTransaction.begin()
SCNTransaction.setAnimationDuration(0.5)

material.emission.contents = UIColor.blackColor()

SCNTransaction.commit()
}

material.emission.contents = UIColor.redColor()

SCNTransaction.commit()
}
}

override func shouldAutorotate() -> Bool {
return true
}

override func prefersStatusBarHidden() -> Bool {
return true
}

override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
if UIDevice.currentDevice().userInterfaceIdiom == .Phone {
return .AllButUpsideDown
} else {
return .All
}
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Release any cached data, images, etc that aren't in use.
}

}

那么这是一个错误吗?我应该只相信模拟器吗?这是呈现 View Controller 的糟糕方式吗?为什么会在设备上崩溃?

最佳答案

UIViewController 没有不带参数的初始化器。你应该使用这个:

public init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?)

并为其提供 xib 文件的名称。模拟器可以处理这种情况 - 它猜测 xib,但设备没有。

关于swift - 模拟器工作但再次呈现 UiViewController 时设备崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39080579/

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