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

2014/10/14

Randomly spawn & remove actor

eekje87 eekje87

2014/10/14

#
Hello, for a school project we are making a game. Its a duck hunt twist. The game is still in progress.. Anyways, we are facing a problem. We want to have a Goat spawn at the left bottom corner of the screen, that walks to the right corner of the screen and then dissapears. It will only make goat sounds and look funny, further has this actor no actual part of the game. We tried creating a for loop with a random number for spawning it. The problem is that once it has spawned and done what its suposed to do, it never respawns again... First we tried this code thinking that this method will be re-executed multiple times when having the game "run".. but it doesn't.
    public void randomGoat()
    {

            int goat = Greenfoot.getRandomNumber(20);
            if (goat <= 5)
           {
            addObject(new Goat(), 51, 553);
           } 
    }
Then we tried with a for loop, giving the condition a very high number so it will repeat itself more often but in that case it overloads the CPU making the game run with insane lag. Can anyone help out please :)
Super_Hippo Super_Hippo

2014/10/14

#
Do you call the method from the act-method? Then it will be executed every act-cycle. Probably you will face a lot of goats then. Is that the plan? If you only want one goat on the screen, then you could just spawn it once and set it to the left edge when it reached the right edge.
danpost danpost

2014/10/14

#
If you want only one goat at a time with a time-gap between them, you should probably then have an instance reference field for the Goat object in your world subclass. In the world constructor (or in the field declaration line), assign a Goat object to the field. Then you can ask 'if goat is not in world and random number is such-and-such' for adding the goat into the world (not creating a new Goat object; but,, using the Goat object that is stored in the field).
eekje87 eekje87

2014/10/15

#
@ Super_Hippo I tried calling the method from my World class in my populate world method, i thought "act" also applies to the world, i didn't knew i can (addobject) a new Goat straight from the Goat.class. Perhaps i am still too amature for this. @danpost Well a goat every 20 seconds approx is my goal, and perhaps 1 at the time would be a good point. Unfortunately i don't quite understand how to make a goat stored in a field and use it the way you do. I have just started working with Greenfoot 5-6 weeks now.
Super_Hippo Super_Hippo

2014/10/15

#
'act' does apply to the world. But you need to create a 'public void act() {...}' method yourself. And in this method, you are adding a new Goat (or you call the randomGoat method from there). If you want to add a new Goat after a defined period of time, you can either use an (act-cycle) timer or use the system time. If you use the system time, you can have exactly 20 seconds, but it doesn't matter how fast your scenario runs, so if you lower the execution speed for example, there will be more Goats after a while, because they won't disappear before the next one is added.
eekje87 eekje87

2014/10/15

#
Great! I just created a act method in the world class, and this way it works! super nice, thanks a million !! :) I changed the condition to randomnumber (400) and if that number == 1 a goat spawns.. works like a charm only sometimes 2-3 spawn at the same time.. no clue how to use a timer yet, sounds complicated.
Super_Hippo Super_Hippo

2014/10/15

#
You can use a timer like this:
private int timer = 0;

public void act()
{
   timer++;
   
   if (timer > 400)
   //spawn a Goat every 400 act-cycles (about 7 seconds with speed at 50)
   {
      addObject(new Goat(), 51, 553);
      timer=0;
   }
   // **OR** //
   if (timer > 100 && Greenfoot.getRandomNumber(300)==0)
  //spawn a Goat when the random number is 0, but only, if the timer is above 100 (there won't be 2 Goats spawned at the same time)
   {
      addObject(new Goat(), 51, 553);
      timer=0;
   } 
}
Of course you can change all numbers to a value which fits for your scenario.
danpost danpost

2014/10/15

#
I was suggesting something like this:
// world instance field
Goat goat = new Goat();

// in act method
if (goat.getWorld() == null && Greenfoot.getRandomNumber(200) == 1)
    addObject(goat, 51, 553);
You need to login to post a reply.