This site requires JavaScript, please enable it in your browser!
Greenfoot back
macaroon72
macaroon72 wrote ...

2020/4/24

Why is ".class" not compiling?

1
2
danpost danpost

2020/4/24

#
Show your revised Magic8Ball class codes.
macaroon72 macaroon72

2020/4/24

#
Okay, here you go.
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);
        }
    }   
}
Super_Hippo Super_Hippo

2020/4/24

#
Well, you call methods on m before you check if it is null. You have to check ‘m != null’ before calling getX() and getY() on it.
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
    }
}
You need to login to post a reply.
1
2