App freezes when adding child to SKCropNode

64 Views Asked by At

When I try to add child that exists in my scene to cropNode, app freezes. But it does not freeze if I add this child as image in assets. When I try to load that scene, app does not respond to clicks.

I use Swift 5, SpriteKit 11 and Xcode 10.

that code freezes the app:

self.screenApp1 = childNode(withName: "screenapp1") as? SKSpriteNode
let cropMask = SKSpriteNode(imageNamed: "crop")
let cropNode = SKCropNode()

cropNode.position = CGPoint(x: 0, y: 0)
cropNode.maskNode = cropMask
cropNode.zPosition = 4

let child = self.screenApp1
child.size = CGSize(width: 260, height: 346)

cropNode.addChild(child)
addChild(cropNode)

and that does not:

let cropMask = SKSpriteNode(imageNamed: "crop")
let cropNode = SKCropNode()

cropNode.position = CGPoint(x: 0, y: 0)
cropNode.maskNode = cropMask
cropNode.zPosition = 4

let child = SKSpriteNode(imageNamed: "screenapp1")
child.size = CGSize(width: 260, height: 346)

cropNode.addChild(child)
addChild(cropNode)
1

There are 1 best solutions below

2
rafulin On BEST ANSWER

I fixed it. All I needed was one line:

child.removeFromParent()

after

let child = self.screenApp1

It works perfectly now.