Sprite Kit iOS 7.1 crash on removeFromParent

前端 未结 5 662
眼角桃花
眼角桃花 2020-11-28 10:20

I have updated iPad Air to 7.1 and Xcode to 5.1. Xcode wanted to update my project to recommended settings, I agreed.

After that my game began crashing in a couple o

相关标签:
5条回答
  • 2020-11-28 10:33

    I also crash with SKShapeNode when its parent remove from superview in iOS 7.1

    My work around is set SKShapeNode property to nil before it remove from parent

    0 讨论(0)
  • 2020-11-28 10:38

    Not sure if my situation exactly mimics yours, but I was getting the same error (with the same stack trace) and realized that I had set up two classes that were each keeping the SKShapeNode object as properties. I'm pretty sure that when I called removeFromParent to remove object node in ClassA the object was deallocated. Then, in ClassB, I called self.node = aNewNode (keep in mind that the object that self.node pointed to had been deallocated), the auto-synthesized setter tried to de-allocate node for a second time.

    I thought ARC was supposed to keep track of all this, but the bug is very sporadic so I'm honestly not 100% sure what's going on. I have an SKSpriteNode with the same pattern and I have never seen it causing this error. My fix right now has been to make the ClassB property weak, so it's not a problem if self.node has already been deallocated. Hope that helps!

    0 讨论(0)
  • 2020-11-28 10:40

    It seems to only happen on iOS 7.1. Not sure if it is an Apple bug, but this seems to fix it:

    - (void)removeFromParent
    {
        [self.aShapeNode removeFromParent];
        self.aShapeNode = nil;
    
        [super removeFromParent];
    }
    
    0 讨论(0)
  • 2020-11-28 10:49

    I am also experiencing the crash. However, the solutions to override the removeFromParent method and setting the SKShapeNode to nil didn't work for me.

    I did discover that my crash was the combination of:

    1. Having a SKShapeNode (either a subclass OR a child of a SKNode)

    2. Having a SKPhysicsNode attached to a node (attached to a parent SKNode OR the SKShapeNode).

    3. Calling the method removeFromParent

    The solution I ended up using was overriding the removeFromParent method in my SKShapeNode subclass with the following:

    - (void)removeFromParent
    {
        if( self.physicsBody != nil )
        {
            self.physicsBody = nil;
        }
    
        [super removeFromParent];
    }
    
    0 讨论(0)
  • 2020-11-28 10:56

    You can call this method before you dealloc (or present new scene) the SKShapeNodes

    - (void)cleanUpChildrenAndRemove:(SKNode*)node {
        for (SKNode *child in node.children) {
            [self cleanUpChildrenAndRemove:child];
        }
        [node removeFromParent];
    }
    

    I had the same problem but that solved it. My original question:

    SKShapeNode producing crash sometimes on dealloc EXC_BAD_ACCESS

    0 讨论(0)
提交回复
热议问题