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

2014/5/1

Changing locations of objects after an intersection

gamer121 gamer121

2014/5/1

#
Hi, Whenever, my Player intersects Home (located at 9,9), I want to add ONE lizard to the world and change the location of the Home to (0,0) and when the Player intersects the Home at that point, another ONE lizard is added and the location of Home goes back to (9,9) and so on. Is there a way to do this? My code is shown below but does not work. import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) public class Home extends Actor { private int k; public Home()
{
      GreenfootImage img = new GreenfootImage(40,40);
      setImage(img);
  }
  public void act()
  
  {
      Actor player = getOneIntersectingObject(Player.class);
      if(player!=null) {
            getWorld().addObject(new Lizard(),Greenfoot.getRandomNumber(10),Greenfoot.getRandomNumber(10));
            setLocation(0,0);
            k=1;
        } 
      if(k==1) {
           setLocation(9,9);
        }
  }
}
danpost danpost

2014/5/1

#
You do not need the 'k' field. You can determine where to move the home by one of its coordinates (either 9 or 0).
Actor player = getOneIntersectingObject(Player.class);
if (player != null)
{
    int newCoord = 0;
    if (getX() == 0) newCoord = 9;
    setLocation(newCoord, newCoord);
    getWorld().addObject(new Lizard(), Greenfoot.getRandomNumber(10),Greenfoot.getRandomNumber(10));
}
I would use 'int newCoord = (1-getX()/9)*9;' to replace lines 4 AND 5, myself.
davmac davmac

2014/5/1

#
I would use 'int newCoord = (1-getX()/9)*9;' to replace lines 4 AND 5, myself.
I'm curious why you say this. Do you find this easier to read or do you think it would be more efficient? I personally find the original easier to understand in terms of intention, and suspect it would be more efficient also. At machine code level, on most architectures, the original two lines would probably boil down to 2 machine instructions (after the getX() method call was complete): a compare instruction, and a conditional move instruction - whereas the single line you suggest, if compiled literally, requires a relatively expensive divide instruction followed by a multiply instruction (which is also relatively expensive, though less so than a divide). Of course picking at the cost of individual instructions is not really constructive here (it's unlikely to make a measurable performance difference, regardless of which alternative you choose). My main point is about the readability of the code. If you just wanted to get the code down to one line, one possibility is:
int newCoord = (getX() == 0) ? 9 : 0;
This still makes the intention clear, I think.
gamer121 gamer121

2014/5/1

#
So now, I want to add 10 new food objects into the world randomly every time the Player gets to Home. How do I add the food randomly without copy and pasting the code several times? My Food class code is shown below. Thanks so much for the help.
You need to login to post a reply.