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

2014/1/15

Problem with spawning!

GreenAnthony GreenAnthony

2014/1/15

#
Hi, I kinda know how to program many different things so one of my friends asked me to make an easy game for him that is supposed to measure the reaction time. I made a chance system which is a little complicated but now that I have finished it I started the game just to find an error message coming up when the "GreenCar" reaches the end of the world even though it is just supposed to respawn... it tells me the problem is the spawning part but I don't see the problem so could you guys help me out? Thanks! Here are the codes for the "GreenCar" class which is the one with errors:
int Space = 10;
    int AmountChance = 10;
    int Amount = 10;
    /**
     * Act - do whatever the GreenCar wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        move(15);
        if (atWorldEdge())
        {
            getWorld().removeObject(this);
            Space = Greenfoot.getRandomNumber(5);
        }
        if (Space==1)
        {
            AmountChance = Greenfoot.getRandomNumber(3);
            getWorld().addObject(new GreenCar(), 50, 350);
            return;
            
            
        }
        if (Space==2)
        {
            AmountChance = Greenfoot.getRandomNumber(3);
            getWorld().addObject(new GreenCar(), 50, 50);
            return;
            
            
        }
        if (Space==3)
        {
            AmountChance = Greenfoot.getRandomNumber(3);
            getWorld().addObject(new GreenCar(), 50, 150); 
            return;
            
            
        }
        if (Space==0)
        {
            AmountChance = Greenfoot.getRandomNumber(3);
            getWorld().addObject(new GreenCar(), 50, 450);
            return;
        }
        if (AmountChance==2)
        {
            Amount = Greenfoot.getRandomNumber(6);
        }
        if (Amount==1)
        {
            getWorld().addObject(new GreenCar(), 50, 50);
            return;
        }
        if (Amount==2)
        {
            getWorld().addObject(new GreenCar(), 50, 150);
            return;
        }
        if (Amount==3)
        {
            getWorld().addObject(new GreenCar(), 50, 350);
            return;
        }
        if (Amount==4)
        {
            getWorld().addObject(new GreenCar(), 50, 450);
            return;
        }
        if (Amount==5)
        {
            getWorld().addObject(new GreenCar(), 50, 250);
            return;
        }
        if (canSee(GreenCar.class))
        {
           getWorld().removeObject(this);
        }
        if (canSee(RedCar.class))
        {
            eat(RedCar.class);
        }
         if (canSee(BlueCar.class))
        {
            eat(BlueCar.class);
        }
         if (canSee(Car.class))
        {
            eat(RedCar.class);
        }
    }    
}
Gevater_Tod4711 Gevater_Tod4711

2014/1/15

#
The problem is that when the car reaches the worlds edge it gets removed from the world. After removing the car from the world the method getWorld() will return null. So the code in line 19, 27, 35, ... will cause a NullPointerException because getWorld().addObject in this case is the same as null.addObject. To avoid this problem you can check whether the car is in a world before executing the rest of the code:
public void act()   
    {  
        move(15);  
        if (atWorldEdge())  
        {  
            getWorld().removeObject(this);  
            Space = Greenfoot.getRandomNumber(5);  
        }
        if (getWorld() != null) {  
        if (Space==1)  
        {  
            AmountChance = Greenfoot.getRandomNumber(3);  
            getWorld().addObject(new GreenCar(), 50, 350);  
            return;  
              
              
        }  
        if (Space==2)  
        {  
            AmountChance = Greenfoot.getRandomNumber(3);  
            getWorld().addObject(new GreenCar(), 50, 50);  
            return;  
              
              
        }  
        if (Space==3)  
        {  
            AmountChance = Greenfoot.getRandomNumber(3);  
            getWorld().addObject(new GreenCar(), 50, 150);   
            return;  
              
              
        }  
        if (Space==0)  
        {  
            AmountChance = Greenfoot.getRandomNumber(3);  
            getWorld().addObject(new GreenCar(), 50, 450);  
            return;  
        }  
        if (AmountChance==2)  
        {  
            Amount = Greenfoot.getRandomNumber(6);  
        }  
        if (Amount==1)  
        {  
            getWorld().addObject(new GreenCar(), 50, 50);  
            return;  
        }  
        if (Amount==2)  
        {  
            getWorld().addObject(new GreenCar(), 50, 150);  
            return;  
        }  
        if (Amount==3)  
        {  
            getWorld().addObject(new GreenCar(), 50, 350);  
            return;  
        }  
        if (Amount==4)  
        {  
            getWorld().addObject(new GreenCar(), 50, 450);  
            return;  
        }  
        if (Amount==5)  
        {  
            getWorld().addObject(new GreenCar(), 50, 250);  
            return;  
        }  
        if (canSee(GreenCar.class))  
        {  
           getWorld().removeObject(this);  
        }  
        if (canSee(RedCar.class))  
        {  
            eat(RedCar.class);  
        }  
         if (canSee(BlueCar.class))  
        {  
            eat(BlueCar.class);  
        }  
         if (canSee(Car.class))  
        {  
            eat(RedCar.class);  
        }  
        }
    }      
}  
danpost danpost

2014/1/15

#
Another problem exists with seeing Green cars. If a Green car is seen, the 'this' actor is removed from the world and the next 'canSee' for the Red car will cause an IllegalStateException error. Move the Green car 'if' block to the end, after checking all the other color cars. NOTE: with the spawn code in the class of the type of object being spawned: (1) no objects of this type will be spawned in none of this type are in the world; and (2) the more of this type that are in the world, the faster they will spawn. All in all, this seems like it would act like a population growth scenario, as is. If you want the rate of spawning to remain constant, move the spawning code into the world class. Also, it would seem like the spawning code could be simplified a bit.
You need to login to post a reply.