Reputation: 11746
I noticed a weird behavior in my Swift project and reproduced it on a empty SpriteKit Project that way:
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()
}
}
It crash before the app start and all I can see is this :
My config is xCode6-Beta6, iPad Mini Retina with iOS8-Beta5 and OSX 10.9.4.
I also reproduced the bug in the simulators; with xCode6-Beta5; and moving the code in touchesBegan
method
Uncommenting the line sprite.removeFromParent()
make the bug disappear.
Upvotes: 3
Views: 1166
Reputation: 7245
IMPORTANT: this bug has been corrected since iOS 8.1, be sure to update AND make your app unavailable for iOS 8.0 and prior.
I found out what happens... and it should be some Apple employe which did a mistake. Explainations :
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 it :
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 very simply when you call .removeFromParent()
Apple's code check for equality in code with ==
like they would do in Objective-C. But since it's Swift you should do ===
to check for object equality rather than ==
, so a dumb mistake.
Congratz you found a bug in SpriteKit Code, go fill a form at Apple :D
Upvotes: 6
Reputation: 177
This happen only when SKNode have same name and same class. Set to SKNode unique name.
My Code: - (void) removeItemWithIdentifier:(FoodsIdentifiers) identifier{
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 SAME NAME BUG!!!!!!!!
Upvotes: 0