Following RayWenderlich tutorial i'm having some problem to be able to create a block that makes the bullet bounce in it. But the most disgunting thing is not to be able to detect the collision between the bullet and the brick. Now the bullet pass the brick like it doesn't exist.
Here is the definition of bullet and brick: Bullet:
bullet = [SKSpriteNode spriteNodeWithColor:[SKColor greenColor] size:kBulletSize];
bullet.name = kShipFiredBulletName;
bullet.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:bullet.frame.size];
bullet.physicsBody.dynamic = YES;
bullet.physicsBody.restitution = 1.0;
bullet.physicsBody.affectedByGravity = NO;
bullet.physicsBody.categoryBitMask = kShipFiredBulletCategory;
bullet.physicsBody.contactTestBitMask = kInvaderCategory;
bullet.physicsBody.collisionBitMask = 0x0;
Brick:
SKNode* rebote;
rebote = [SKSpriteNode spriteNodeWithColor:[SKColor orangeColor] size:kBouncetSize];
rebote.position = CGPointMake(550, 500);
rebote.name = @"kBounceBlock";
rebote.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:rebote.frame.size];
rebote.physicsBody.restitution = 1.0;
rebote.physicsBody.friction = 0.0;
rebote.physicsBody.dynamic = NO;
rebote.physicsBody.affectedByGravity = YES;
rebote.physicsBody.categoryBitMask = kShipFiredBulletCategory;
rebote.physicsBody.contactTestBitMask = kInvaderCategory;
rebote.physicsBody.collisionBitMask = 0x0;
[self addChild:rebote];
And here the collision detection method :
-(void)handleContact:(SKPhysicsContact*)contact
{
// Ensure you haven't already handled this contact and removed its nodes
if (!contact.bodyA.node.parent || !contact.bodyB.node.parent)
return;
NSArray* nodeNames = @[contact.bodyA.node.name, contact.bodyB.node.name];
if ([nodeNames containsObject:kShipName] && [nodeNames containsObject:kInvaderFiredBulletName])
{
// Invader bullet hit a ship
[self runAction:[SKAction playSoundFileNamed:@"ShipHit.wav" waitForCompletion:NO]];
//1
[self adjustShipHealthBy:-0.334f];
if (self.shipHealth <= 0.0f)
{
//2
[contact.bodyA.node removeFromParent];
[contact.bodyB.node removeFromParent];
}
else
{
//3
SKNode* ship = [self childNodeWithName:kShipName];
ship.alpha = self.shipHealth;
if (contact.bodyA.node == ship)
[contact.bodyB.node removeFromParent];
else
[contact.bodyA.node removeFromParent];
}
}
else if ([nodeNames containsObject:kInvaderName] && [nodeNames containsObject:kShipFiredBulletName])
{
NSLog(@"Contacto entre bala e Invader.");
// Ship bullet hit an invader
[self runAction:[SKAction playSoundFileNamed:@"InvaderHit.wav" waitForCompletion:NO]];
[contact.bodyA.node removeFromParent];
[contact.bodyB.node removeFromParent];
//4
[self adjustScoreBy:100];
}
if ([nodeNames containsObject:@"kBounceBlock"])
NSLog(@"Contacto entre el cuerpo naranja !!");
}
Whaat i'm doing wrong ? I defined the restitution property in bullet and in the brick too. The brick is not dynamic and both are in the same 'collisionBitMask'.
I'm pretty lost. Thanks in advance
Set up proper
contactTestBitMask
andcollisionBitMask
forBrick
andBullet
.For some reasons you have
collisionBitMask
set to0x0
, which means that node will not collide with any object at all.Also, you have specified a wrong
categoryBitMask
forBrick
(the same as forBullet
). You should create a separate category for it: