Mike Simz
Mike Simz

Reputation: 4026

Sprite Kit Remove SKSpriteNodes within Array within -Update method

I have an array of sprites in sprite kit and in my update method I check to see if these sprites fall off the bottom of the screen. If they do, I remove them from their parent and I remove them from their array..what I am wondering is if they way I am doing it if it is the best practice? Is it going to slow down performance? If so, what is the best way to go about accomplishing this?

Here's my code:

 -(void)update:(CFTimeInterval)currentTime {

     // Detect when animals fall off bottom of screen
NSMutableArray *animalsToDiscard = [NSMutableArray array];
for (SKSpriteNode* node in animalsArray) {
    if (node.position.y < -node.size.height) {

        NSLog(@"Remove Animal - Lose Life");
        [node removeFromParent];
        [animalsToDiscard addObject:node];
    }
}
[animalsArray removeObjectsInArray:animalsToDiscard];
}

Upvotes: 1

Views: 228

Answers (1)

meisenman
meisenman

Reputation: 1828

You should use the built in physics world to help solve this task. Basically, you would build a "floor" and if your animal contacts it, remove the animal node.

https://stackoverflow.com/a/24195006/2494064

In this link, your "animal" would be the equivalent of "bubble". The code will work if you make that substitution.

Upvotes: 1

Related Questions