Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 模拟器工作,但再次显示UiViewController时设备崩溃_Swift_Scenekit - Fatal编程技术网

Swift 模拟器工作,但再次显示UiViewController时设备崩溃

Swift 模拟器工作,但再次显示UiViewController时设备崩溃,swift,scenekit,Swift,Scenekit,我一直在开发一款不同级别的游戏,你可以通过菜单访问它。级别为3D并使用SceneKit,菜单为2D并使用SpriteKit 游戏从一级开始,这是一个UiViewController,当你击败它时,菜单出现。如果你按下菜单中的一个按钮(上面写着“Level 1”),你将被引导回到Level 1,但是由于某种原因,当在iPhone上再次加载Level 1时,它崩溃了,但是模拟器工作正常 我这里有一个简单的示例代码,它做了几乎相同的事情,只在设备上崩溃: import UIKit import Qua

我一直在开发一款不同级别的游戏,你可以通过菜单访问它。级别为3D并使用SceneKit,菜单为2D并使用SpriteKit

游戏从一级开始,这是一个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.
}

}

那么这是一只虫子吗?我应该相信模拟器吗?这是表示视图控制器的糟糕方式吗?为什么它会在设备上崩溃?

UIViewController没有无参数的初始值设定项。你应该使用这个:

public init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?)
并为其提供xib文件的名称。
模拟器可以处理这种情况-它猜测xib,但设备没有。

我觉得这像是内存泄漏,因为随着程序运行,它会逐渐变慢。每次呈现场景/视图时,都会实例化该场景/视图

计时器具有对场景的字符串引用。它似乎在上一个VC上启动presentViewController()。每次(每2秒?为什么?)你都会消耗另一个VC和场景

我看不出这堆东西从哪里冒出来


泄漏仪器显示了什么?

模拟器和设备是否运行相同版本的IOS?您应该检查设备日志,看看错误是什么。我是否将其放在UIViewController中?我如何为它提供xib文件的名称?什么是xib文件(可能是故事板文件,但我从不使用故事板)?xib不是故事板文件-它是视图控制器的IB文件。使用此初始值设定项而不是“GameViewController()”。crush消息是什么?它给出了一个线程10:signal SGABRT,控制台说:断言失败:(renderSize.x!=0),函数-[SCNRenderContextMetal\u setupDescriptor:forPass:isFinalTechnique:],文件/BuildRoot/Library/Caches/com.apple.xbs/Sources/SceneKit/SceneKit-332.6/Sources/Core3DRuntime/NewRenderer/SCNRenderContextMetal.mm,第688行。这意味着您的
SCNView
有一个坏帧(空宽度和/或高度),并且没有要渲染的像素。@m说明我使SCNView.frame的宽度和高度为1像素,并且没有崩溃!但在展示了几次场景后,它开始落后很多,然后崩溃并发出了记忆警告。有没有更好的方法多次呈现UIViewController?