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

2016/9/20

Need help with my refill loop.

Green1604 Green1604

2016/9/20

#
Hello, I´m making a zombie survive game and I need some help with my repopulate loop. I want to make 3 waves of 5 zombies and after the third wave no more zombies should spawn, but after the third wave the zombies spawn again and again. Here is my Loop:
    public void refill()
    {
        int i=0;
        if (getObjects(Zombie1.class).size() == 0)
        {
           if(rp<=0)
           {
               for(i=0;i<5;i++)
               {
                   addObject(new Zombie1(),Greenfoot.getRandomNumber(900), Greenfoot.getRandomNumber(900));
               }
               if(getObjects(Zombie1.class).size() == 0)
               {
                   rp++;
                }
           }
           if(rp>0 && rp<2)
           {
               for(i=0;i<5;i++)
               {
                   addObject(new Zombie1(),Greenfoot.getRandomNumber(900), Greenfoot.getRandomNumber(900));
               }
               if(getObjects(Zombie1.class).size() == 0)
               {
                   rp++;
                }
           }
           if(rp>1 && rp<3)
           {
               for(i=0;i<5;i++)
               {
                   addObject(new Zombie1(),Greenfoot.getRandomNumber(900), Greenfoot.getRandomNumber(900));
               }
               if(getObjects(Zombie1.class).size() == 0)
               {
                   rp++;
                }
           }
        }
    }
Would be nice if someone could help me. Thanks
danpost danpost

2016/9/21

#
It would be much simpler like this:
public void refill()
{
    if (rp < 3 && getObjects(Zombie1.class).isEmpty())
    {
        for (int i=0; i<5; i++)
        {
            addObject(new Zombie1(), Greenfoot.getRandomNumber(900), Greenfoot.getRandomNumber(900));
        }
        rp++;
    }
}
Green1604 Green1604

2016/9/21

#
Thanks that worked. Now I´ve added a fourth wave with a boss zombie. But after I defeted the boss greenfoot chrashes. This is the Boss1 code:
public class Boss1 extends Enemys
{
    private int health;
    /**
     * Act - do whatever the Boss1 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        follow();
        move();
        remove();
        spawn();
    }    
    public Boss1()
    {
        this.health = 500;
    }
    public void remove()
    {
        Actor bullet = getOneIntersectingObject(Bullet.class);
        if(bullet !=null)
        {
            getWorld().removeObject(bullet);
            if((health -= 25) <= 0)
            {
                getWorld().removeObject(this);
            }
        }
    }
    public void move()
    {
        if(health>=300)
        {
            move(2);
        }
        else if(health<300 && health>=100)
        { 
            move(4);
        }
        else if(health<100 && health>0)
        {
            move(5);
        }
    }
    public void spawn()
    {
        
        if (getWorld().getObjects(Zombie1.class).isEmpty())
       {
          for(int i=0; i<5; i++)
          {
              getWorld().addObject(new Zombie1(),Greenfoot.getRandomNumber(900), Greenfoot.getRandomNumber(900));
            }
       }

    }
}
This is the Greenfoot error: java.lang.NullPointerException at Boss1.spawn(Boss1.java:58) at Boss1.act(Boss1.java:22) at greenfoot.core.Simulation.actActor(Simulation.java:604) at greenfoot.core.Simulation.runOneLoop(Simulation.java:562) at greenfoot.core.Simulation.runContent(Simulation.java:221) at greenfoot.core.Simulation.run(Simulation.java:211)
danpost danpost

2016/9/21

#
In your Boss1 class, switch lines 12 and 13. You cannot call a method on 'getWorld' when the actor is not in a world (see 'spawn' method).
Green1604 Green1604

2016/9/21

#
Thanks that worked.
You need to login to post a reply.