Game Development Reference
The next section of code simply sets the stage boundaries. It uses the simple bounce formula you
looked at earlier in the chapter to bounce the bugs off the stage edges when they get too close, like
Another very simple bit of code that results in surprising complexity! The last thing you did was set up
the collision detection code for the platforms:
This is the same as the code you used to detect a collision between the platforms and the lh]uan,
except that there has been no bounce value assigned.
The next step is to make the bugs aware of their environment. Bugs don't like frogs, so let's make
them fly away if they come within 50 pixels of the frog.
The collision detection follows the same logic that you used in the original collision detection code
from Chapter 7. It checks whether a bug is within 50 pixels of the frog on the x axis. If it is, it checks
whether the bug is within 50 pixels of the frog on the y axis. If that's true as well, the bug is too close
to the frog, and it changes its direction.
1. Add the following code to the kj>qcIkra event handler: