How can I make my coin disappear when my player collides with it?
I dont know if i should use the SKNode instead or what :/.
Please help I cant seem to figure it out
CODE:
-(void)spawnCoin {
SKNode* coinNode = [SKNode node];
coinNode.position = CGPointMake(self.frame.size.width + _buildTexture1.size.width + 150 + (arc4random() % 100), 0 );
coinNode.zPosition = -10;
CGFloat y = arc4random() % (NSInteger)( self.frame.size.height / 2 ) + 40;
SKAction* spin = [SKAction repeatActionForever:[SKAction animateWithTextures:@[ _coinTexture1, _coinTexture2, _coinTexture3, _coinTexture4, _coinTexture5, _coinTexture6, _coinTexture7, _coinTexture8, _coinTexture9, _coinTexture10] timePerFrame:0.05]];
coin = [SKSpriteNode spriteNodeWithTexture:_coinTexture10];
[coin runAction:spin];
[coin setScale:1];
coin.position = CGPointMake( 0, y );
coin.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:coin.size];
coin.physicsBody.dynamic = NO;
coin.physicsBody.categoryBitMask = coinCategory;
coin.physicsBody.contactTestBitMask = playerCategory;
[coinNode addChild:coin];
[coinNode runAction:_moveCoinAndRemove];
[_coins addChild:coinNode];
}
- (void)didBeginContact:(SKPhysicsContact *)contact {
if( _moving.speed > 0 ) {
if( ( contact.bodyA.categoryBitMask & coinCategory ) == coinCategory || ( contact.bodyB.categoryBitMask & coinCategory ) == coinCategory ) {
//I have Tried [coin removeAllChildren];
_score++;
_scoreLabelNode.text = [NSString stringWithFormat:@"%ld", (long)_score];
}
First get coin node. It can be either contact.bodyA.node or contact.bodyB.node. For Example:-
SKNode* coinNode = contact.bodyA.node;
[coinNode removeFromParent]; //This should work
If you want to just hide the node, then use
coinNode.hidden = YES;
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