Search code examples
sprite-kitios8sknode

SKNode removeFromParent sometimes removes extra children


I have found that one of my SKNodes in my game removes extra children when one of its children calls removeFromParent. To verify this, I overrode removeFromParent and added the following assertion that verifies that only one child was removed. The assertion fails:

- (void) removeFromParent {
SKNode *parent = self.parent;
NSUInteger startingCount = [self.parent.children count];
[super removeFromParent];
if (parent) {
    NSAssert([[parent children] count] == startingCount - 1, @"Wrong number of children after removing one child!");
}

}

I've filed a bug report for this, but I'm now stuck on it because I don't have a workaround. I'm wondering if anyone else has encountered this and might have a workaround for it.


Solution

  • Okay, I found a workaround after following a hunch.

    My child nodes were returning YES for isEqual:, even though they were different objects and I had not overridden isEqual: in my SKSpriteNode subclass. It seems that the implementation of removeFromParent uses isEqual:, and when non-equal nodes returned YES, it was removing more than one.

    My workaround was to override isEqual: with the following:

    - (BOOL) isEqual:(id)object {
        return self == object;
    }