Reputation: 2335
What I'm try to do, is replicate some digital instrument gauge ...using Spritekit..
On my project I have to use a SpriteKit Scene as material for Scenekit SCNnode.
In order to do that I create a sub class of SKscene and and apply it as "material.diffuse.contents" of the SCNNode.
all working fine, I can see my SKscene as material of the SCNNode.
Here the issue:
I try to animate/replicate the correct indication of the instrument based of some value my app received from other source.
To do so, i decide to use the SKsceneDelegate and use the method "update(_ currentTime: TimeInterval)"
simply the update method look for the sknode of the green needle "fulcro" remove it , and make a new line calculating the angle and new coordinate for the new line.
override func update(_ currentTime: TimeInterval) {
guard let fulcroToRemove = self.childNode(withName: "fulcro") else {return}
fulcroToRemove.removeFromParent() // issue here...
let fulcro = SKNode()
fulcro.name = "fulcro"
fulcro.position = CGPoint(x: 300, y: 260)
let line = makeLine(startPos: CGPoint(x: 0, y: 0),
endPos: getCoordinate(angleDeg: getCurrentEGT()),
name: "EGT_LINE")
fulcro.addChild(line)
self.addChild(fulcro)
}
... if I try this SKscene itself in a project only with Spritekit all working fine.. I can see my needle increase the value correctly when the data source increase.
But if use the same SKscene as material of SceneKit getting the following error:
look like I'm removing something not correctly .. and I can see fulcroToRemove.removeFromParent() is the line trigger the error.
But why ??How I can remove the line to avoid the crash?
Upvotes: 1
Views: 127
Reputation: 758
The problem is that removeFromParent()
can only be called on the main queue. Xcode should tell you that in the console when it displays the stacktrace.
There is an additional issue:
When you call removeFromParent()
on a node, its reference counter is decremented, and the node is possibly deinitialised unless you keep a reference. This can cause a rare crash in the renderer because the node is being rendered and freed at the same time. I've seen this in my current project.
Therefore I have implemented a simple garbage collection method for removed SKNode
instances. If you release nodes in SKScene.update(:)
there shouldn't be a crash anymore.
Upvotes: 0