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

2017/1/16

Problem with adding objects

cooknj cooknj

2017/1/16

#
    public void Prepare()
    {
        Bar bar = new Bar();
        addObject(bar,4,9);
        Player player = new Player();
        addObject(player,0,7);     
        for (int bi = 1; bi<6; bi++)
        {
        int banditx = Greenfoot.getRandomNumber(7);
        int bandity = Greenfoot.getRandomNumber(7);
        Actor bandit;
        bandit = getOneObjectAtOffset(banditx, bandity, Bandit.class);
        if (bandit==null)
        {
          addObject(bandit, banditx, bandity);
        }
    }
    }
I'm trying to make the bandit spawn in a cell without another one spawning in the same cell.
Super_Hippo Super_Hippo

2017/1/16

#
Try to use this instead of the current lines 11-16.
if (getObjectsAt(banditx, bandity, Bandit.class).isEmpty())
{
    addObject(new Bandit(), banditx, bandity);
}
else
{
    bi--;
}
You need to login to post a reply.