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

2016/9/25

Two Enemies that follow error.

Samas Samas

2016/9/25

#
So in my program I want to have two wolves that chase my sheep. The problem is I get an error that stops my program from running.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Wolf here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Wolf extends Actor
    {
    
    public static int slowTimer;


    /**
     * Act - do whatever the Wolf wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
     
    public void act()  
        {  
        int dist = 1000;  
        Actor closest = null;  
        Actor Sean = (Actor)getWorld().getObjects(Sean.class).get(0); // The error occurs on this line when one Wolf gets Sean (the Sheep)
        if(!getObjectsInRange(dist, Sean.class).isEmpty())  
            {  
            for (Object obj: getObjectsInRange(dist, Sean.class))  
                {                                  
                int seanDist = (int) Math.hypot(Sean.getX() - getX(), Sean.getY() - getY());  
                if (closest == null || seanDist< dist)  
                {  
                    closest = Sean;  
                    dist = seanDist;  
                    }                 
                }  
            turnTowards(closest.getX(),closest.getY());  
            }  
        if (!getIntersectingObjects(Rock.class).isEmpty()) 
            {
            move(-4);
            }
        if(getOneIntersectingObject(Sean.class) != null)  
            {  
             Actor Wolf = getOneIntersectingObject(Sean.class);
             if( Sean == Wolf )
                {  
                getWorld().removeObject(Sean);    
                }  
           }
        if (slowTimer > 0)
            { 
            move(2);
            }
        else 
            {
            move(3);
            }
 
        /** in World class act method (or a method it calls) */
        if (Wolf.slowTimer > 0)
            {
             Wolf.slowTimer--;
            }
       
        }
    
    }
Both of the wolves have this code. I know it is because when one wolf eats the sheep the other has no idea what to do. Is there anyway to fix it?
Samas Samas

2016/9/25

#
the error occurs on line 24
danpost danpost

2016/9/25

#
Samas wrote...
the error occurs on line 24
You could replace lines 24 and 25 with the following lines:
Actor Sean = null;
if (!getWorld().getObjects(Sean.class).isEmpty())
{
    Sean = (Actor)getWorld().getObjects(Sean.class).get(0);
}
if (Sean != null && !getObjectsInRange(dist, Sean.class).isEmpty())
This ensures that the list returned by 'getObjects' is not empty (line 2) before trying to access the first element (line 4) from the list (which was causing the error). However, you really do not need all this, provided the 'closeness' loop was properly coded. Line 27 has each Sean object placed in the variable called 'obj'; yet, nowhere in the loop are you utilizing the elements from the list (you are comparing the same information throughout the loop, which does not get you anywhere). Replace lines 24 through 37 with the following:
for (Object obj: getObjectsInRange(dist, Sean.class))  
{
    Actor actor = (Actor) obj;
    int seanDist = (int) Math.hypot(actor.getX() - getX(), actor..getY() - getY());  
    if (closest == null || seanDist< dist)  
    {  
        closest = actor;  
        dist = seanDist;  
    }                 
}  
if (closest != null) // this ensures a Sean object is in the world
{
    turnTowards(closest.getX(),closest.getY());
}
danpost danpost

2016/9/25

#
I think lines 42 through 49 can be replaced simply with this:
if (closest != null && intersects(closest)
{
    getWorld().removeObject(closest);
}
Samas Samas

2016/9/25

#
Thank you!
You need to login to post a reply.