Book Image

Creative Greenfoot: RAW

By : Michael Haungs
Book Image

Creative Greenfoot: RAW

By: Michael Haungs

Overview of this book

Table of Contents (17 chapters)
Creative Greenfoot
About the Author
About the Reviewers

Hidden-sprite collision detection methods

One flaw with the getOneObjectAtOffets() and getObjectsAtOffset() methods is that they only check the granularity of a single pixel. If an object of interest is one pixel above or below the offset provided to these methods, then no collision will be detected. In fact, in this implementation, if you allow the simulation to run until the zombies reach the houses, you'll notice that some zombies can move past the houses. This is because the pixel-only check fails between houses. One way to handle this deficiency is to use hidden-sprite collision detection. Figure 7 illustrates this method.

Figure 7: This shows the use of a hidden sprite to check for collisions.

In the hidden-sprite method, you use another Actor class to test for collisions. Figure 7 shows a Zombie object using a smaller, auxiliary Actor class to determine if a collision occurred with the flower. While the hidden sprite is shown as a translucent red rectangle, in practice, we would set...