Game Development Reference
In-Depth Information
Figure 16-40.
The finished .collide() method is error-free and only 19 lines of code, and is ready for further optimiz-
ation
If you use a
Run Project
work process, and test your game code now, you will
not only hear the audio, you will also see the Prop objects disappear upon collision
with the InvinciBagel object. I'm going to leave the audio in this code for now, so you
can test to see if these Actor objects are really gone by have the InvinciBagel run (or
fly) over the area where the Prop object was to make sure (using your ears) that it's
really gone. Next, we'll optimize the code even more, by implementing the
if(collide(object))
approach that I mentioned earlier, having the .collide() method
simply return a
true
(collision detected) or
false
(no collision detected) value, as a
properly behaved Boolean method should.
Optimizing
Collision
Detection
Processing:
if(collide(object))
To optimize the collision detection process, we'll move code that is executed on colli-
sion up to the
.checkCollision()
method, inside of an
if(collide(object)){}
conditional if block of Java code. This allows us to eliminate a Boolean
colli-
sionDetect
variable from the collide() method, making it much more streamlined. All
we are doing now in the .collide() method is pass the
return true;
statement back
up to the calling entity, in this case, the .checkCollision() method, if a collision is de-