我一直致力于通过菜单访问不同级别的游戏。级别为3D并使用SceneKit,菜单为2D并使用SpriteKit。
游戏从1级开始,这是一个UiViewController,当你击败它时,会出现菜单。如果您在菜单中按下一个按钮(表示“级别1”),您将被定向回到1级,但由于某种原因,在iPhone上再次加载1级时,它会崩溃,但模拟器工作正常。 / p>
我这里有一个简单的示例代码,它几乎完全相同,只在设备上崩溃:
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.
}
}
这是一个错误吗?我应该相信模拟器吗?这是一种呈现视图控制器的坏方法吗?为什么它会在设备上崩溃?
答案 0 :(得分:0)
UIViewController没有没有参数的初始化程序。你应该使用这个:
public init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?)
并为其提供xib文件的名称。 模拟器可以处理这种情况 - 它猜测xib,但设备没有。
答案 1 :(得分:0)
这闻起来像是一个内存泄漏给我,因为它随着程序的运行而逐渐变慢。看起来每次呈现时都会实例化场景/视图。
计时器具有对场景的字符串引用。它似乎在前一个VC上触发presentViewController()。每次通过(每2秒?为什么?),你将消耗另一个VC和场景。
我不知道堆栈弹出的位置。
泄漏仪器显示什么?