Now we have all the core elements of the game defined, we need to add collision handling for the ball hitting the bricks. If we were to stop here, the ball would hit the bricks, and the bricks would go flying off from the force of the impact. We really want the brick to be destroyed, so we will revisit the update
method we described earlier in the Losing Your Ball section. Immediately after the
if…else
statement in the update
method, we add a couple of clauses:
Filename: BRPlayfieldLayer.mm
(inside init
method)
else if (spriteA != NULL && spriteB != NULL) { // Sprite A = ball, Sprite B = Block if (spriteA.tag == kBall && spriteB.tag == kBrick) { if (std::find(toDestroy.begin(), toDestroy.end(), bodyB) == toDestroy.end()) { toDestroy.push_back(bodyB); } } // Sprite B = block, Sprite A = ball else if (spriteA.tag == kBrick && spriteB.tag == kBall) { if (std::find(toDestroy.begin(), toDestroy.end(), ...