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

2022/3/31

How do I move without rotating my actor?

Billkingtang Billkingtang

2022/3/31

#
I want to implement code where a zombie chases after a person but I don't want the image for the actor rotate because it looks weird. A solution would be appreciated. Thanks!
Billkingtang Billkingtang

2022/3/31

#
I figured it out but now I have another question. I want my zombie to chase people of the parent class but zombie is included within it too. The parent class "Pedestrian" has 3 sublasses "Citizen", "Police", and "Zombie." I want the zombie to search for the either the nearest citizen or police. Right now in the code it only searches for the closest Citizen. Help would be appreciated!
 private void targetClosestHuman()
    {
        int dist = 400;
    Actor closest = null;
    if(!getObjectsInRange(dist, Citizen.class).isEmpty())
    {
        for (Object obj: getObjectsInRange(dist, Citizen.class))
        {
            Actor guy = (Actor) obj;
            int guyDist = (int)Math.hypot(guy.getX() - getX(), guy.getY() - getY());
            if (closest == null || guyDist< dist)
            {
                closest = guy;
                dist = guyDist;
            }
        }
        turnTowards(closest.getX(),closest.getY());
    }   
    }
Super_Hippo Super_Hippo

2022/3/31

#
I think the easiest is to get a list with the Pedestrian class. In the for-loop, ignore actors which are Zombies using “instanceof”.
Billkingtang Billkingtang

2022/3/31

#
I'm not sure what instanceOf is and how to use it properly
Super_Hippo Super_Hippo

2022/3/31

#
if (obj instanceof Zombie) continue;
//rest of for-loop
Billkingtang Billkingtang

2022/3/31

#
It worked thanks so much!!!!!!
Billkingtang Billkingtang

2022/3/31

#
These are two separate parts of the same class where the error occured.
 public void act()
    {
        if (hp > 0){ //if zombie has more then 0 hp it will remain active
            hp -= VehicleWorld.ZOMBIE_DEATH_RATE;
            hpBar.update(hp);
            
            //Move
            
            targetClosestHuman();

        }else if(getY() < 100){
            getWorld().removeObject(this);
        }else if ( getY() > 550){
             getWorld().removeObject(this);
        }else{ //dies when it reaches 0 hp
            getWorld().removeObject(this);
        }
    }
  private void targetClosestHuman()
    {
        int dist = 100;
        Actor closest = null;
        if(!getObjectsInRange(dist, Pedestrian.class).isEmpty())
        {
            for (Object obj: getObjectsInRange(dist, Pedestrian.class))
            {
                if (obj instanceof Zombie) continue; // loops through all actors in Pedestrian class and ignores all Zombies
                Actor guy = (Actor) obj;
                int guyDist = (int)Math.hypot(guy.getX() - getX(), guy.getY() - getY());
                if (closest == null || guyDist < dist)
                {
                    closest = guy;
                    dist = guyDist;
                }
                
            }
            //So my actor doesnt appear to rotate when he moves after a human
        
            setRotation(rotation); //Before turning and moving it is set to 0
            move(maxSpeed); //Move

            //After turning and moving
            turnTowards(closest.getX(),closest.getY()); //Turn
            rotation = getRotation(); //Get Rotation and do the turn
            setRotation(0); //Set the Rotation back to 0
        }   
    

    }
Im not sure why but after a bit from running I run into a error. java.lang.NullPointerException at Zombie.targetClosestHuman(Zombie.java:105) at Zombie.act(Zombie.java:48) at greenfoot.core.Simulation.actActor(Simulation.java:567) at greenfoot.core.Simulation.runOneLoop(Simulation.java:530) at greenfoot.core.Simulation.runContent(Simulation.java:193) at greenfoot.core.Simulation.run(Simulation.java:183)
Billkingtang Billkingtang

2022/4/1

#
The problem looks like it stems from the if (obj instanceof Zombie) continue; If I remove that then I dont run into the problem. Is there any possible solutions? I think the problem is when there are no pedestrians nearby the loop doesn't know what to do? EDIT: I found a solution to the problem. I simply used a conditional to check if the obj was null.
You need to login to post a reply.