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

2017/3/21

How do I make more than one zombie spawn in at the same time?

Grace20102 Grace20102

2017/3/21

#
I would really appreciate any help!
Super_Hippo Super_Hippo

2017/3/21

#
addObject(new Zombie(), 100, 100);
addObject(new Zombie(), 100, 100);

//same as
for (int i=0; i<2; i++) addObject(new Zombie(), 100, 100);
Do not use: (it only tries to add the same zombie twice)
Zombie zombie = new Zombie();
for (int i=0; i<2; i++) addObject(zombie, 100, 100);
Grace20102 Grace20102

2017/3/21

#
Thanks Super_Hippo! I have another issue. I want the zombies to move randomly along the X axis. Do you think you could help?
Super_Hippo Super_Hippo

2017/3/21

#
What have you tried so far? (Use 'code' tags when posting code.) How exactly should it move randomly? What rules should it follow?
Grace20102 Grace20102

2017/3/21

#
public class Zombie extends Actor
{
   public void act()
   { 
       move(2);
      if(Greenfoot.getRandomNumber (20) == 1)
      {
        setRotation(180);
        setImage(new GreenfootImage("Zombie Right.png"));
      }
   }
}
I just want the zombies to go left and right.
Super_Hippo Super_Hippo

2017/3/21

#
Maybe this works if you change line 8 to
turn(180);
Grace20102 Grace20102

2017/3/21

#
Super_Hippo wrote...
Maybe this works if you change line 8 to
turn(180);
This didn't seem to work, it just made them go upside down.
Super_Hippo Super_Hippo

2017/3/21

#
If you don't want the actor to rotate, try this:
private int direction = 1;

public void act()
{
    move(direction*2);
    if (Greenfoot.getRandomNumber(20)==0) direction *= -1;
}
Grace20102 Grace20102

2017/3/21

#
Super_Hippo wrote...
If you don't want the actor to rotate, try this:
private int direction = 1;

public void act()
{
    move(direction*2);
    if (Greenfoot.getRandomNumber(20)==0) direction *= -1;
}
It worked! Thank you so much!
You need to login to post a reply.