I'm wondering what code I should add to stop rocks from piling up at the bottom of the screen and have them removed when resting (not moving).
Here is the code that creates the rocks. If you need any other code please let me know :)
-(void)addRock {
SKSpriteNode *rock = [SKSpriteNode spriteNodeWithImageNamed:@"asteroid"];
rock.position = CGPointMake ([self makeRandomXWBetween:0 and:self.size.width], self.size.height);
rock.name = @"rock";
rock.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:rock.size];
rock.physicsBody.usesPreciseCollisionDetection = YES;
rock.physicsBody.categoryBitMask = rockCategory;
rock.physicsBody.contactTestBitMask = dodgerCategory;
[self addChild:rock];
}
-(void)makeRocks {
SKAction *makeRocks = [SKAction sequence: @[
[SKAction performSelector:@selector(addRock) onTarget:self],
[SKAction waitForDuration:0.3 withRange:0.0]
]];
[self runAction: [SKAction repeatActionForever:makeRocks]];
}
Thanks in advance for any help!
Best Regards, Louis.
Since you want the rocks to be removed when they are resting, they can be removed when their velocity property is zero.
You can check so in the -update method.
-(void)update:(CFTimeInterval)currentTime {
[self enumerateChildNodesWithName:@"rock" usingBlock:^(SKNode *node, BOOL *stop){
if ((node.physicsBody.velocity.dx == 0) && (node.physicsBody.velocity.dy == 0) && (node.position.y < self.size.height)) {
[node removeFromParent];
}
}];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With