Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SceneKit presentScene(_withTransition:incomingPointOfView completionHandler) crash with dynamically loaded SCNScene

Tags:

crash

scenekit

I'm trying to transition from one scene to another, but when I call presentScene there is a crash! The scenes are not stored in a class or referenced, they are loaded directly into the presentScene call.

Screenshot of crash in Xcode: crash screencap

My simple minimal project is here: https://dl.dropboxusercontent.com/u/6979623/SceneKitTransitionTest.zip

MKScene is just a subclass of SCNScene, because I would like to know when a scene is deinited to be sure that is it.

self.gameView!.scene = MKScene(named:"art.scnassets/scene1.scn")

then later I call

let scnView:SCNView = self.gameView! as SCNView
let skTransition:SKTransition = SKTransition.crossFadeWithDuration(1.0)
skTransition.pausesIncomingScene = false
skTransition.pausesOutgoingScene = false
self.sceneToggler = !self.sceneToggler
// transition
scnView.presentScene((self.sceneToggler ? MKScene(named:"art.scnassets/scene1.scn")! : MKScene(named:"art.scnassets/scene2.scn")!), withTransition:skTransition, incomingPointOfView:nil, completionHandler:nil)

If I keep a reference to the scene in my class then it works – but that's not what I want. I just want to transition to a different scene and leave the current scene behind deinited.

Why is this crashing? It seems like a simply task…

like image 864
StackUnderflow Avatar asked Dec 08 '25 10:12

StackUnderflow


2 Answers

That's a bug in SceneKit. Workaround: keep a reference to the outgoing scene before calling "presentScene" and release it after that call.

like image 77
Toyos Avatar answered Dec 10 '25 01:12

Toyos


I was able to reproduce your crash with a somewhat simpler project. It does not use MKScene and does not use notifications to trigger the transition. It crashes on the second attempt to load.

I have filed this at https://bugreport.apple.com as rdar://24012973, which you my wish to dupe, along with your longer project.

Here's my simplified ViewController.swift. Switching between the SCNScene properties (lines 25/29) or on-the-fly loads (lines 24/28) toggles between correct and crashing behavior. That is,

        nextScene = SCNScene(named:"art.scnassets/scene2.scn")!

fails, and

        nextScene = scene2!

works.

//  ViewController.swift
import Cocoa
import SceneKit
import SpriteKit

class ViewController: NSViewController {

@IBOutlet weak var sceneView: SCNView!
private var sceneToggler:Bool = false
private var scene1: SCNScene? = SCNScene(named:"art.scnassets/scene1.scn")
private var scene2: SCNScene? = SCNScene(named:"art.scnassets/scene2.scn")

private func nextSceneToLoad() -> SCNScene {
    let nextScene: SCNScene

    if (sceneToggler) {
        //nextScene = SCNScene(named:"art.scnassets/scene1.scn")!
        nextScene = scene1!
        print ("scene1")
    }
    else {
        nextScene = SCNScene(named:"art.scnassets/scene2.scn")!
        //nextScene = scene2!
        print ("scene2")
    }
    print (nextScene)
    sceneToggler = !sceneToggler
    return nextScene
}

override func mouseUp(theEvent: NSEvent) {

    let skTransition:SKTransition = SKTransition.fadeWithDuration(5.0)
    skTransition.pausesIncomingScene = false
    skTransition.pausesOutgoingScene = false
    sceneView.presentScene(nextSceneToLoad(),
        withTransition:skTransition,
        incomingPointOfView:nil, completionHandler:nil)


    super.mouseUp(theEvent)
}
}
like image 29
Hal Mueller Avatar answered Dec 10 '25 01:12

Hal Mueller