

In little crab, the Crab, Worm and Lobster classes were all subclasses of the Animal class. First, we will go over some methods and discuss their intended.

The Animal class contained a move method and a turn method so that you could make these objects move and turn. The Animal class also had the atWorldEdge and canSee methods to allow the objects to detect when they'd reached the edge of the world or when they had encountered another object. We are going to go through all the methods provided by Greenfoot to do collision detection. The Mover class (from the Greenfoot web site) also had move and turn methods and an atWorldEdge method for detecting the edge of the world. Mover does not have a collision detection method, however.Īll objects in Greenfoot scenarios are subclasses of the Actor class, so they always have access to its methods. The Actor class uses the setLocation method to move an object to a specified location in the world. That is, the center point of the image of the object is positioned on cell (x,y). To move an object continuously, you need to move if from where it currently is, to a new position a few cells away. In order to move objects at an angle, you need to change both the x and y coordinates, like this: moves the object 5 cells to the right, and The actor's getX() and getY() methods tell you where an object is currently positioned. The difference between the Mover and Actor classes is that the Mover class always moves the object in the direction it is facing. The Actor class moves the object independent of the direction it's facing. Instead we'd move an object like this:īetter yet, we should store the vertical and horizontal displacements in instance variables so that we can change the direction of an object by changing the sign of either of these variables:Īnd then we could move the object like this: We commonly don't use temporary variables to hold the object's current x and y coordinates. SetLocation( getX() + hSpeed, getY() + vSpeed )

If we were to change hSpeed to a negative number, the object would move left and similarly, if we changed vSpeed to a negative number, the object would move upwards. The Actor class contains the getRotation and s etRotation methods for turning an object. The rotation of an object is the degree angle it's facing, with 0 degrees being EAST (or to the right). To turn an Actor object, we need to get its current rotation and add or subtract a specified number of degrees to/from that rotation: If we created the following method in one of our Actor subclasses. We could use the method call turn(5) to turn the object 5 degrees clockwise, or turn(-10) to turn the object 10 degrees counter-clockwise.Īgain, the Actor subclass' movement would be independent of the direction the object is facing, so. Would cause the object to face SOUTH but still move EAST 5 cells.
