Error when attempting to remove node from parent using fast enumeration

后端 未结 1 1283
没有蜡笔的小新
没有蜡笔的小新 2020-12-19 09:09

After Upgrading to iOS 8 b3 and Xcode 6 b3 I get an error in the didSimulatePhysics method:

[self enumerateChildNodesWithName:@\"name\" usingBlock:^(SKNode *         


        
相关标签:
1条回答
  • 2020-12-19 09:42

    Behavior may change between iOS versions. It may have actually crashed at some point or very rarely even in Xcode 5, you just didn't get to see it.

    The problem is easily circumvented by delaying the execution of the removeFromParent method. This should do the trick because actions are evaluated at a specific point in the game loop rather than instantaneously:

    [self enumerateChildNodesWithName:@"name" usingBlock:^(SKNode *node, BOOL *stop) {
        if (node.position.y < 0 || node.position.x>320 || node.position.x<0) {
            [node runAction:[SKAction removeFromParent]];
        }
    }];
    

    If this won't work use the "old trick": filling an NSMutableArray with to-be-deleted items and removing the nodes in that array after enumeration:

    NSMutableArray* toBeDeleted = [NSMutableArray array];
    
    [self enumerateChildNodesWithName:@"name" usingBlock:^(SKNode *node, BOOL *stop) {
        if (node.position.y < 0 || node.position.x>320 || node.position.x<0) {
            [toBeDeleted addObject:node];
        }
    }];
    
    for (CCNode* node in toBeDeleted)
    {
        [node removeFromParent];
    }
    
    0 讨论(0)
提交回复
热议问题