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

2018/4/2

Adding Objects

Heet_Patel Heet_Patel

2018/4/2

#
I am creating a new game. I need a code which adds object from one place every few seconds. The name of the actor where its going to come from is Tutankhamun and the actor spawning is called Bee. Could someone help me with the code. The world is called Desert.
Recorsi Recorsi

2018/4/2

#
In the Tutankhamun class you can write
Bee bee = new Bee();
getWorld().addObject(bee, getX(), getY());
Yehuda Yehuda

2018/4/2

#
Use a timer. When the timer reaches its target value spawn a Bee and reset the timer value.
/* class Tutankhamun */
private int spawnTimer = 250
// in act
spawnTimer--;
if (spawnTimer == 0) {
    getWorld().addObject(new Bee(), getX(), getY());
    spawnTimer = 250;
}
Heet_Patel Heet_Patel

2018/4/3

#
tnx but i dont want one ever round but i want it to spawn one every few seconds.
Heet_Patel Heet_Patel

2018/4/3

#
tnx guys
Yehuda Yehuda

2018/4/5

#
I don't understand. Is everything fine now, and if it isn't, I don't understand what you what different?
Heet_Patel Heet_Patel

2018/4/5

#
yeah its working fine tnx for your help
Heet_Patel Heet_Patel

2018/4/5

#
Now the problem is that when i put the code for randomly moving the bees in the actor bee it doesn't move but it goes crazy.
Heet_Patel Heet_Patel

2018/4/5

#
move(2);
       if (Greenfoot.getRandomNumber(100) < 10)
       {
           turn(Greenfoot.getRandomNumber(90) - 45);
       }   
       if (getX() <= 5 || getX() >= getWorld().getHeight() - 5)
       {
           turn(180);
       }
       if (getY() <= 5 || getY() >= getWorld().getHeight() - 5)
       {
           turn(180);
       }
Heet_Patel Heet_Patel

2018/4/5

#
i changed the second getHeight into getWidth but it still doesn't work.
move(2);
       if (Greenfoot.getRandomNumber(100) < 10)
       {
           turn(Greenfoot.getRandomNumber(90) - 45);
       }   
       if (getX() <= 5 || getX() >= getWorld().getHeight() - 5)
       {
           turn(180);
       }
       if (getY() <= 5 || getY() >= getWorld().getWidth() - 5)
       {
           turn(180);
       }
danpost danpost

2018/4/5

#
Heet_Patel wrote...
i changed the second getHeight into getWidth but it still doesn't work. << Code Omitted >>
You changed the wrong one.
Heet_Patel Heet_Patel

2018/4/5

#
ok tnx
Heet_Patel Heet_Patel

2018/4/5

#
yeah its working
Heet_Patel Heet_Patel

2018/4/12

#
Guys, is there a code i can add to the previous codeof spawning the characters every few seconds where i can limit the amount of bees spawning.
/* class Tutankhamun */
private int spawnTimer = 250
// in act
spawnTimer--;
if (spawnTimer == 0) {
    getWorld().addObject(new Bee(), getX(), getY());
    spawnTimer = 250;
}
This is the code where i want the code to limit the amount of bees spawn
danpost danpost

2018/4/12

#
Well, if you wanted to limit the number of bees in the world to, say 50, then you could use:
// in act
if (spawnTimer > 0) spawnTimer--;
if (spawnTimer == 0 && getWorld().getObjects(Bee.class).size() < 50)
{
    getWorld().addObject(new Bee(), getX(), getY());
    spawnTimer = 250;
}
You need to login to post a reply.