Question

I've been working on some SpriteKit tutorials. I understand the whole collision thing and have verified with NSLog that a collision is being registered between my two objects. However for some really strange reason my sprite is not being created (or rather shown) when it's done during didBeginContact.

- (void)didBeginContact:(SKPhysicsContact *)contact
{
    uint32_t collision = (contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask);
     if (collision == (CNPhysicsCategoryPlayer | CNPhysicsCategoryRock))
     {
         NSLog(@"ouch");
         SKSpriteNode *bigOuch = [SKSpriteNode spriteNodeWithImageNamed:@"star"];
         bigOuch.position = CGPointMake(200, 200);
         [self addChild:bigOuch];
     }
}

I get the ouch log message but no sprite appears.

I have tried the same (sprite creation) code in other parts of my program and have no issues. What am I doing wrong?

Was it helpful?

Solution

I was stuck on the exact same issue a while back. You can create a SKSpriteNode and add it to the view but it does not get displayed. The short of it is that I ended up creating an array and adding any sprites I needed to create during the didBeginContact phase. During the update phase I checked the array and added them to my view. Just remember to empty the array after you are done. Otherwise you will end up with the same sprite being added over and over again.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top