1 / 6

Checking for Collisions in Greenfoot

Checking for Collisions in Greenfoot. Greenfoot provides a two important methods that allow an object to detect whether it is intersecting with another object: atWorldEdge( ) This method returns a “true” value if an object is at the edge of the world This method is in the “Mover” class

benito
Download Presentation

Checking for Collisions in Greenfoot

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Checking for Collisionsin Greenfoot

  2. Greenfoot provides a two important methods that allow an object to detect whether it is intersecting with another object: • atWorldEdge( ) • This method returns a “true” value if an object is at the edge of the world • This method is in the “Mover” class • getOneIntersectingObject(CLASS) • This method returns the object that is being intersected.

  3. To make an object disappear if it is at the edge of the world… • Type the following in the class’ act( ) method: if (atWorldEdge() == true) { getWorld().removeObject(this); } -removeObject(this) will remove your object. -atWorldEdge( ) will return a boolean value

  4. To detect if an object is touching another object you will use the following method: This will method will return the object that is being touched. getOneIntersectingObject(CLASS); This is class of the objects for which you are looking.

  5. Here is an example of how you would use that method: public void act() { checkKeys(); checkCollision(); } public void checkCollision() { Actor collided; collided = getOneIntersectingObject(UFO.class); if (collided != null) { //This will remove the object getWorld().removeObject(collided); } }

  6. “collided” is a variable of the type Actor. It will store an Actor object. This method says, “Am I touching a UFO object?” If it is, put that UFO object in the variable “collided.” public void checkCollision() { Actor collided; collided = getOneIntersectingObject(UFO.class); if (collided != null) { //This will remove the object getWorld().removeObject(collided); } } This method removes the “collided” object from the world. If “collided” has an object in it, then do the following…

More Related