SKNode.removeFromParent () EXC_BAD_ACCESS
I noticed strange behavior in my Swift project and reproduced it on an empty SpriteKit project like this:
class GameScene: SKScene {
override func didMoveToView(view: SKView) {
let sprite = SKSpriteNode(imageNamed:"Spaceship")
self.addChild(sprite)
//sprite.removeFromParent()
let sprite2 = SKSpriteNode(imageNamed:"Spaceship")
self.addChild(sprite2)
sprite2.removeFromParent()
}
}
The crash before starting the app and all I can see is this:
My configuration is xCode6-Beta6, iPad Mini Retina with iOS8-Beta5 and OSX 10.9.4. I also reproduced the bug in simulators; with xCode6-Beta5; and moving the code to the touchesBegan
method
Uncommenting the line sprite.removeFromParent()
will clear the error.
source to share
IMPORTANT: This bug has been fixed since the release of iOS 8.1, be sure to update AND make your app unavailable for iOS 8.0 and earlier.
I found out what's going on ... and it must be some kind of Apple that made the mistake. Explanations:
let sprite = SKSpriteNode(imageNamed:"Spaceship")
self.addChild(sprite)
let sprite2 = SKSpriteNode(imageNamed:"Spaceship")
self.addChild(sprite2)
sprite2.removeFromParent()
println( sprite == sprite2 ) // Returns "true"
// Then crash
And if you do this:
let sprite = SKSpriteNode(imageNamed:"Spaceship")
sprite.name = "1"
self.addChild(sprite)
let sprite2 = SKSpriteNode(imageNamed:"Spaceship")
sprite2.name = "2"
self.addChild(sprite2)
sprite2.removeFromParent()
println( sprite == sprite2 ) // Returns "false"
// Then all is right, no crash
I think it's just that when you call .removeFromParent()
Apple code, check for equality in the code using ==
as it would in Objective-C. But since it's Swift, you have to do ===
to check for object equality , not ==
, so that's a silly mistake.
Congratulations, you found a bug in the SpriteKit code, go fill out the form at Apple : D
source to share
This only happens when SKNode has the same name and the same class. Set a unique SKNode.
My code: - (void) removeItemWithIdentifier: (FoodsIdentifiers) {
NSArray *items = [foodItemHolderNode children];
int count = (int)[items count];
for (int i = 0; i < count; i++) {
FoodItemObject *foodItem = [items objectAtIndex:i];
int itemID = [[foodItem.objectDictionary objectForKey:FOODITEM_IDENTIFIER_KEY] intValue];
if (itemID == identifier) {
[foodItem removeFromParent];
break;
}
}
// NSString *name = [NSString stringWithFormat:@"ID%i", (int)identifier];
//SKNode *item = [foodItemHolderNode childNodeWithName:name];
//[item removeFromParent];
warning REMOVE FROM PARENT WITH NAME NAME BUG !!!!!!!!
source to share