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

2015/9/28

Java isTouching/removeObject error

Gosuo Gosuo

2015/9/28

#
So I tried to code my ship so that when it touches an asteroid it's going to be removed.
1
2
3
4
5
6
7
8
9
public void destruction()
    {
        Ship s = (Ship) getOneIntersectingObject(Ship.class);
        if (s.isTouching(Asteroid.class))
        {
            getWorld().removeObject(this);
        }
        return;
    }
But when I try to run the game I get an error:
java.lang.NullPointerException at Ship.act(Ship.java:18) at greenfoot.core.Simulation.actActor(Simulation.java:594) at greenfoot.core.Simulation.runOneLoop(Simulation.java:552) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205)
I looked in the discuss section for answers, but couldn't find a problem that fits mine.
davmac davmac

2015/9/28

#
The NullPointerException is happening in your act method - " at Ship.act(Ship.java:18)" - so that is where you need to look for the problem. If you post the code for the act method here (and indicate which line the exception occurs on) then someone may be able to help you.
danpost danpost

2015/9/28

#
Gosuo wrote...
So I tried to code my ship so that when it touches an asteroid it's going to be removed. < Code Omitted >
I am at a loss as to where (what class) the code given is in. Line 3 suggests it is in the Asteroid class (getting an intersecting Ship object).. Line 4 will not execute unless the code is in the Ship class (the 'isTouching' method has protected access). Line 6 also suggest the code is in the Ship class (the Ship is what you want removed -- correct?).
Gosuo Gosuo

2015/9/28

#
This was my first discussion on this forum and I put it online on sunday and of course no admin works on sunday. I solved the problem by just use the method: removeTouching(Ship.class); in the act method of the asteroid. But now I have a diffrent problem with the construction of the world. Don't know if you guys can help me there to, but on my laptop the world construct with no problem. Here on my PC the construction takes to long. Whis is my world class:
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
40
41
42
43
44
45
import greenfoot.*;
 
/**
 * Write a description of class Weltraum here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Weltraum extends World
{
 
    /**
     * Constructor for objects of class Weltraum.
     *
     */
    public Weltraum()
    {   
        // Create a new world with 1024x768 cells with a cell size of 1x1 pixels.
        super(400, 600, 1);
        spawnShip();
         
    }
     
    public void act()
    {
       int nRandomNumber = Greenfoot.getRandomNumber(400);
       int nRandomNumber2 = Greenfoot.getRandomNumber(50);
       if(nRandomNumber2 == 4)
       {
           spawnAsteroid(nRandomNumber, 0);
       }
    }
     
    public void spawnAsteroid(int x, int y)
    {
        Asteroid a1 = new Asteroid();
        addObject(a1, x, y);
    }
     
    public void spawnShip()
    {
        Ship s1 = new Ship();
        addObject(s1, 200, 550);
    }
}
I know the random spawn isn't solved perfectly. To be honest I started programming Java 1 or 2 weeks ago. xD Asteroid:
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
import greenfoot.*;
 
/**
 * Write a description of class Asteroid here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Asteroid extends Actor
{
    /**
     * Act - do whatever the Asteroid wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
     
    public Asteroid()
    {
        setImage("images/Asteroid_benutzbar.png");
    }
     
   public void act()
   {
       flyBy();
       if(getY() == 599)
       {
           getWorld().removeObject(this);
       }
       removeTouching(Ship.class);
   }
    
   public void flyBy()
   {
       int myX = getX();
       int myY = getY();
       setLocation(myX, myY+2);
   }
    
}
And finally the ship class:
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import greenfoot.*;
/**
 * Write a description of class Ship here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Ship extends Actor
{
    /**
     * Act - do whatever the Ship wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
       movement();
       while(getWorld().getObjects(Ship.class).size() != 0)
       {
           if(getWorld().getObjects(Ship.class).size() != 0)
           {
               getWorld().setBackground("images/game_over.png");
           }
       }
    }
     
    private void moveLeft()
    {
        int myX = getX();
        int myY = getY();
        setLocation(myX-5, myY);
        Greenfoot.delay(1);
    }
     
    private void moveRight()
    {
        int myX = getX();
        int myY = getY();
        setLocation(myX+5, myY);
        Greenfoot.delay(1);
    }
     
    public void movement()
    {
        if(Greenfoot.isKeyDown("left"))
        {
            moveLeft();
        }
        else if(Greenfoot.isKeyDown("right"))
        {
            moveRight();
        }
    }
}
My class is very slow (I started programming C++ for a while and I know all the commands we do now and I wanted to employ me. So please don't judge sloppy code. xD
Gosuo Gosuo

2015/9/28

#
I noticed maybe I could ommit the while-loop
1
2
3
4
5
6
7
while(getWorld().getObjects(Ship.class).size() != 0)
       {
           if(getWorld().getObjects(Ship.class).size() != 0)
           {
               getWorld().setBackground("images/game_over.png");
           }
       }
And now I can construct the world, but neither the act() method of the world class nor the act() method of the asteroid or the ship executes! :(
danpost danpost

2015/9/29

#
Gosuo wrote...
I noticed maybe I could ommit the while-loop < Code Omitted > And now I can construct the world, but neither the act() method of the world class nor the act() method of the asteroid or the ship executes! :(
The while-loop is indeed problem code. It is placed in your Ship act method which will, first off, only execute when a Ship object is indeed in the world. Secondly, there is nothing inside the loop that will decrease the number of Ship objects in your world, so the loop will be an 'infinite' loop and run indefinitely, essentially freezing the running of the scenario. There are a couple of issues with your Asteroid class code; one is that you do not need to specify that the image files are in the 'images' folder (it may work at home; but, it will not find the files if you share the scenario); the other is the placement of line 28 ('removeTouching(Ship.class);'), which will cause an 'IllegalStateException' error when the first asteroid reaches the bottom of the window and is removed from the world (either place a 'return;' statement after removing 'this' or move the line in question to the second line of the method). Finally, place the code to check for a Ship object in the world to the Weltraum class act method (ask if the returned List object using 'getObjects' is empty via the 'isEmpty' method of the List class -- not if its size is not zero). Oh, and you do not need to use a loop for this, as a simple 'if' statement will suffice.
Gosuo Gosuo

2015/9/29

#
Man feel so dump right now. I will check your offers soon. But I'm writing an exam tomorrow, so I have to learn for that first.
Gosuo Gosuo

2015/9/29

#
Thanks alot now everything is working prefect. :)
You need to login to post a reply.