Coding the collision detection and playing sounds
Now that we have done so much theory and preparation, we can finish everything off very quickly. We will write code to detect all the various collisions using the RectF
intersects
method, play sounds, and call the required methods of our classes.
The bat and the ball
Add the following highlighted code inside the detectCollisions
method:
private void detectCollisions(){ // Has the bat hit the ball? if(RectF.intersects(mBat.getRect(), mBall.getRect())) { // Realistic-ish bounce mBall.batBounce(mBat.getRect()); mBall.increaseVelocity(); mScore++; mSP.play(mBeepID, 1, 1, 0,...