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

2017/12/2

How do I get an actor to slow down when it hits another actor?

rory rory

2017/12/2

#
Ok so basically I have a sheep and a snake. Whenever the sheep comes in contact with the snake the sheep is supposed to slow down for 1 minute. I literally have no clue on how to do this and I also don't know which actors code to put the code for this in... please help. I can attach my sheep code and snake code below. (ignore anything about flowers or spiders) HERE IS MY SNAKE CODE:
import greenfoot.*; 

/**
 * Snake. A Snake moves forward until it hits the edge of the world, at 
 * which point it turns. If a Snake finds a sheep it takes away the food from the 
 * sheep.
 * If a Snake finds Food it eats it
 * If a Snake is touching a Spider, it turns away.
 * @author () 
 * @version (a version number or a date)
 */
 public class Snake extends Actor
{
      private int foodsEaten;
      public Snake()
      {  
           foodsEaten = 0;
      }
      
      /**
        * Act - do whatever the Snake wants to do.
        */
        public void act() 
      {
           if (isTouching()) 
           {
               eatFood();
           }
           if (isTouchingSpider())
           {
               turn(Greenfoot.getRandomNumber(360));
               move (2);
           }
           if (isAtEdge())
           { 
              turn(Greenfoot.getRandomNumber(360));
           }
           move(1);
      }
   
      public boolean isTouching()
      {
            Actor foody = getOneObjectAtOffset(0, 0, Food.class);
            if (foody == null)
            { 
             return false;
            }
            return true;
      }
      public boolean isTouchingSpider()
      {     Actor Spider = getOneObjectAtOffset(0, 0, Spider.class);
            if (Spider == null)
            {
             return false;
            }
            return true;
      }
      public void eatFood()
      {
            Actor foody = getOneObjectAtOffset(0, 0, Food.class);
            if (foody != null)
            {
               getWorld().removeObject(foody);
               foodsEaten = foodsEaten + 1;
            }
      }
      /**
         * tell how many foods we have eaten.
         */
          public int getfoodseaten()
      { 
            return foodsEaten;
      }
      }
  
HERE IS MY SHEEP CODE.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Sheep here.
 * 
 * @author (
 * @version (a version number or a date)
 */
public class Sheep extends Actor
{
    public int FoodsEaten;
     /**
      * Act - do whatever the Sheep wants to do. This method is called whenever
      * the 'Act' or 'Run' button gets pressed in the environment.
      */
      public void act() 
      
    {
      if (isTouching())
      {
        eatFood();
      }
      if (Greenfoot.isKeyDown ("right"))
      { 
        setRotation(0);move(2);
      }
      else if (Greenfoot.isKeyDown ("left"))
      {
        setRotation(180);
        move(2);
      }
      else if (Greenfoot.isKeyDown("down"))
      {
        setRotation(90);
        move(2);
      }
      else if (Greenfoot.isKeyDown("up"))
      {
        setRotation(270);
        move(2);
      }
    }
   
    public boolean isTouching()
    {
      Actor foody = getOneObjectAtOffset(0, 0, Food.class);
     if (foody == null)
     { 
      return false;
     }
     return true;
    }
    public void eatFood()
    {
      Actor foody = getOneObjectAtOffset(0, 0, Food.class);
      if (foody != null)
      {
       getWorld().removeObject(foody);
       FoodsEaten = FoodsEaten + 1;
      }
    }
    /**
        * tell how many foods we have eaten.
        */
        public int getFoodsEaten()
    {
       return FoodsEaten;
    }
}
If you can help I would be so happy thank you!!!
danpost danpost

2017/12/2

#
You cannot change a hard-coded value (which you currently are using for the speed of the sheep). The value for the speed (currently '2' everywhere for the sheep) needs to be replaced by a variable (a value that can be changed). That variable needs to be maintained by using a field. You will also need a timer field to track the time remaining that the sheep is to run at slow speed.
You need to login to post a reply.