user1377686
user1377686

Reputation: 25

Sprite Kit didBeginContact method not called after ios 7.1 upgrade

After I upgraded my iPhone to ios 7.1, didBeginContact method never get called. Anyone know how to fix it?

We have

In MainScene.h

@interface MainScene : SKScene <SKPhysicsContactDelegate>

In MainScene.m

-(id)initWithSize:(CGSize)size
{
    [self.physicsWorld setGravity:CGVectorMake(0, kGravity)];
    [self.physicsWorld setContactDelegate:self];

     _ground.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:_ground.size];
       [_ground.physicsBody setCategoryBitMask:kGroundCategory];
       [_ground.physicsBody setCollisionBitMask:kPlayerCategory];
       [_ground.physicsBody setAffectedByGravity:NO];
       [_ground.physicsBody setDynamic:NO];

       _player.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(_player.size.width * kHitAreaScale, _player.size.height * kHitAreaScale)];
       [_player.physicsBody setDensity:kDensity];
       [_player.physicsBody setAllowsRotation:NO];

       [_player.physicsBody setCategoryBitMask:kPlayerCategory];
       [_player.physicsBody setContactTestBitMask: kGroundCategory | kMonsterCategory  kTopFloorCategory];
       [_player.physicsBody setCollisionBitMask:kGroundCategory | kMonsterCategory | kTopFloorCategory];
}


The following code in MainScene.m never get called in ios 7.1

- (void)didBeginContact:(SKPhysicsContact *)contact
{
    // firstBody: player
    SKPhysicsBody *firstBody, *secondBody;
    if (contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask)
    {
        firstBody = contact.bodyA;
        secondBody = contact.bodyB;
    }
    else
    {
        firstBody = contact.bodyB;
        secondBody = contact.bodyA;
    }
}

Upvotes: 0

Views: 1007

Answers (3)

ZeMoon
ZeMoon

Reputation: 20274

I encountered this problem only yesterday. I wanted to invert the sprite based on it's movement (right or left) and found that setting xScale disables any collisions/contacts.

However, I used this line every time I set the xScale property and everything went back to normal.

node.physicsBody = node.physicsBody;

Upvotes: 0

user3420889
user3420889

Reputation: 46

Definitely an xScale problem, Doesn't seem to matter if the xScale is set before or after the physics body is set. As long as the xScale is set to -1 my collisions won't work. xScale of 1 works normal.

Upvotes: 1

David
David

Reputation: 217

I had X/Yscale problem after iOS 7.1, maybe this answer also fixes yours.

Just set the physicsBody before X/Yscale.

Upvotes: 0

Related Questions