Show your revised Magic8Ball class codes.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | import greenfoot.*; public class Magic8Ball extends SmoothMover { //Leave this field alone, this will be the Triangle on the screen private TriangleAnswer triangle = new TriangleAnswer(); public void act() { // Ask if the mouse has been dragged on the current object // If so, build a MouseInfo variable and get the mouse from Greenfoot // Teleport to the location of the Mouse // Ask if you are touching a TriangleAnswer // If so, remove the TriangleAnswer MouseInfo m = Greenfoot.getMouseInfo(); int mx = m.getX(); int my = m.getY(); if (m != null && m.getActor() == this ) { setLocation(mx, my); if (isTouching(TriangleAnswer. class )) { getWorld().removeObject(triangle); } } // Ask if the mouse has stopped being dragged on the current object // If so, call triangle's randomize method // Create two variables, xMove and yMove, set them to // a random value from {-100, -99, -98, ... 99, 100 } // add the triangle to the world at (yourX + xMove, yourY + yMove) if (m != null && m.getActor() != this ) { triangle.randomize(); int xMove = ( int )(Math.random()* 201 )- 100 ; int yMove = ( int )(Math.random()* 201 )- 100 ; getWorld().addObject(triangle, getX() + xMove, getY() + yMove); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 | MouseInfo m = Greenfoot.getMouseInfo(); if (m != null ) { if (m.getActor() == this ) { setLocation(m.getX(), m.getY()); removeTouching(TriangleAnswer. class ); } else { // the triangle } } |