I'm attempting to make a game with objects that bounce between the left and right walls of the screen until they reach the bottom.
Currently, my working didBeginContact method looks like this:
- (void)didBeginContact:(SKPhysicsContact *)contact {
uint32_t collision = (contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask);
if (collision == (crabCategory | leftWallCategory)) {
// figure out which crab in the array made the contact...
for(HHCrab *object in crabs) {
if(([object.name isEqualToString:contact.bodyB.node.name]) || ([object.name isEqualToString:contact.bodyA.node.name])) {
[object stop];
[object moveRight];
}
}
} else if (collision == (crabCategory | rightWallCategory)) {
// figure out which crab in the array made the contact...
for(HHCrab *object in crabs) {
if(([object.name isEqualToString:contact.bodyB.node.name]) || ([object.name isEqualToString:contact.bodyA.node.name])) {
[object stop];
[object moveLeft];
}
}
}
When I attempt to add an additional statement:
else if (collision == (crabCategory | bottomWallCategory)) {
// figure out which crab in the array made the contact...
for(HHCrab *object in crabs) {
if(([object.name isEqualToString:contact.bodyB.node.name]) || ([object.name isEqualToString:contact.bodyA.node.name])) {
[object stop];
[object resetPosition];
[object moveLeft];
}
}
}
to reset the crabs position when they reach the bottom of the screen, the crabs will not move. In a previous question I was made aware that this may be due to the fact that drawing the crab was being overwritten by the physics simulation. We solved the issue by creating a Boolean which would be toggled on and off when the crab intersects the bottom wall, and then repositions the crab in the didSimulatePhysics method as such:
-(void)didSimulatePhysics{
if(self.shouldResetPosition){
self.player.position = CGPointMake(CGRectGetMidX(self.frame),CGRectGetMaxY(self.frame)- _player.size.height-30);
self.shouldResetPosition = NO;
}
}
Although this works with one crab, now that I am using an NSMutable Array of Crabs each with a unique name, it does not. I tried moving the Boolean value to the Crab class, so each crab has that property but this did not solve the problem.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…