Okay, so the title makes this seem like a pretty easy fix, but here is the problem. These objects are only manipulated through the loop below, and removing
[SKAction removeFromParent];
solves then problem, only the objects are still living as I can see the memory consumption growing over time, which then reduces frame-rate and also increases CPU.
I'm at a loss as to why this is causing a memory access exception. Thanks in advance.
for (int i=0;i<30; i++) {
SKSpriteNode *particle = [SKSpriteNode spriteNodeWithColor:[SKColor purpleColor] size:CGSizeMake(3, 3)];
particle.zPosition = 0;
particle.position = position; // passed in through method call
[self addChild:particle];
SKAction *move = [SKAction moveByX:ScalarRandomRange(-25, 25) y:ScalarRandomRange(-25, 25) duration:0.1];
SKAction *fadeOut = [SKAction fadeOutWithDuration:0.5];
[particle runAction:[SKAction sequence:@[move,fadeOut]] withKey:@"explosion"];
}
Here is a screenshot of the location of the call- stack where the problem occurs.
I've added the below code in the update method
-(void)update:(CFTimeInterval)currentTime {
/* Called before each frame is rendered */
NSMutableArray *killList = [[NSMutableArray alloc]init];
for (SKNode *node in [self children]) {
if ([node.name isEqualToString:@"particle"]) {
if (!([node actionForKey:@"explosion"])) {
[killList addObject:node];
}
}
}
for (SKNode *node in killList) {
[node removeFromParent];
}
}
and it seems to have fixed the exception issue, but I still noticed the memory slightly climbing, I will need to investigate that in instruments.
This seems like a hackish way of fixing the problem though, as I should be able to use the
[SKAction removeFromParent];
in my sequence.