1

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];
}
Mike Simz
  • 3,976
  • 4
  • 26
  • 43

1 Answers1

1

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.

Community
  • 1
  • 1
meisenman
  • 1,818
  • 1
  • 15
  • 25