Game Development Reference
In-Depth Information
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
this:
^qc*rt&9)-7
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:
bkn$r]ne6ejp9,7e8957e''%
w
?khheoekj*lh]uan=j`Lh]pbkni$^qc(pdeoWlh]pbkni'eY(,(,%7
y
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.
Artificial intelligence
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:
++=npebe_e]hejpahhecaj_a
++Bnkc
eb$$I]pd*]^o$^qc*t)bnkc*t%81,%%
w
eb$I]pd*]^o$^qc*u)bnkc*u%81,%
w
^qc*t'9)^qc*rt7
^qc*u'9)^qc*ru7
^qc*rt&9)-7
^qc*ru&9)-7
pn]_a$^qc*j]ia'6Bnkc%7
y
y
 
Search Nedrilad ::




Custom Search