Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sprite Kit - Hiding SKSpriteNode

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];
 }
like image 686
nickivey Avatar asked Jan 11 '23 02:01

nickivey


1 Answers

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;
like image 198
Srikanth Avatar answered Jan 17 '23 00:01

Srikanth