![]() Add the vector to the object's movement vector.Create a movement vector for the object.To make an SmoothMover subclass object move, you need to do three things: There are times you want an object to move in the direction it's facing (so you'd import the Mover class into your scenario) and other times when you want the object to move independent of the direction it's facing (so you'd use the Actor methods instead).īouncing Balls scenario introduced the SmoothMover and Vector classes to us. Would cause the object to face SOUTH but still move EAST 5 cells. 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. 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. The rotation of an object is the degree angle it's facing, with 0 degrees being EAST (or to the right). The Actor class contains the getRotation and s etRotation methods for turning an object. 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. SetLocation( getX() + hSpeed, getY() + vSpeed ) ![]() 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. The Actor class moves the object independent of the direction it's facing. The difference between the Mover and Actor classes is that the Mover class always moves the object in the direction it is facing. 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. To move an object continuously, you need to move if from where it currently is, to a new position a few cells away. That is, the center point of the image of the object is positioned on cell (x,y). The Actor class uses the setLocation method to move an object to a specified location in 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 Mover class (from the Greenfoot web site) also had move and turn methods and an atWorldEdge method for detecting the edge of the world. ![]() ![]() 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. In little crab, the Crab, Worm and Lobster classes were all subclasses of the Animal class. Using the Animal or Mover support classes If you have completed all the previous scenarios, you should be aware that we have used three methods for moving objects in our scenarios. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |