Adam Carter
Adam Carter

Reputation: 4844

Adding UIGravityBehavior and UIPushBehavior after a collision takes place in iOS 7

I'm messing around with the new physics API in iOS 7.

I decided I would make a simple game, I have a character who can move around the screen at a tap of the finger, periodically, shapes fly up from the bottom and if they hit the character it's game over.

I have implemented all of the above and it works great, however, the only thing that doesn't is the character falling when a shape collides with him.

All collisions work, my problem is trying to make the character fall when the collision takes place. Because I don't want the character to be able to fall before this (lets say he's levitating while trying to avoid shapes) I decided to add the character to the gravity when the collision happens, and it does, but first, the character shoots upwards as the collision animation continues.

Is there any way to prevent the collision animation from happening? Is there a better way?

Here is my code in the collisionBehavior:beganContactForItem:withBoundaryIdentifier:atPoint: method:

//  Main Character
//
if ([item isEqual:self.mainCharacter]) {
    [self.collision removeItem:item];

    //          Push
    //
    UIPushBehavior *pushBehaviour = [[UIPushBehavior alloc] initWithItems:@[self.mainCharacter] mode:UIPushBehaviorModeInstantaneous];
    [pushBehaviour setPushDirection:CGVectorMake(0.0f, 1.0f)];
    [self.animator addBehavior:pushBehaviour];
    [pushBehaviour setActive:YES];

    [self.gravity addItem:item];
}

Upvotes: 0

Views: 924

Answers (1)

Adam Carter
Adam Carter

Reputation: 4844

It turns out I should have been using the collisionBehavior:beganContactForItem:withItem:atPoint: and testing BOTH items in this method to see if it is the mainCharacter

Upvotes: 1

Related Questions