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

2012/6/1

Delay Spawn

SayMyName SayMyName

2012/6/1

#
Hello guys, Currently in my mainWorld I have a method call setNextLevel() which basically add enemies at different position to set up the level. My question is how can I make it that the enemies spawn 1 by 1 after a set time instead of them spawning all at once. I tried to use a counter to count the number of acts and spawn them every x number of acts but it does not seem to work. Here is a sample of my code in the setNextLevel method.
        // Initialize a new Array of red circles with enough spots for 4 red circles per level
        redCircle = new RedCircle [levelCounter * 4];
        for (int i = 0; i < redCircle.length; i++)
        {

            //generate a random number between 1-4 to determine which side the enemies will spawn
            sideCounter = Greenfoot.getRandomNumber(4) + 1;
            // Initialize a new red circle object for each spot in the array
            redCircle[i] = new RedCircle ();

            //if statement to spawn each enemy in their appropriate side
            //spawn at the top
            if (sideCounter == 1) 
            {
                // Add each red circle near the top of the screen
                addObject (redCircle[i], Greenfoot.getRandomNumber(890)+ 40, Greenfoot.getRandomNumber(10)+ 35);
            }
            //spawn on the right side
            else if (sideCounter == 2)
            {
                //Add each red circle near the right side of the screen
                addObject (redCircle[i],Greenfoot.getRandomNumber(20) + 915, Greenfoot.getRandomNumber(600) + 40);
            }
            //spawn at the bottom
            else if (sideCounter == 3)
            {
                //add each red circle near the bottom of the screen
                addObject (redCircle[i],Greenfoot.getRandomNumber(900) + 40 , Greenfoot.getRandomNumber(10) + 620);
            }
            //spawn on the left side
            else if (sideCounter == 4)
            {
                //add each red circle near the left side of the screen
                addObject (redCircle[i],Greenfoot.getRandomNumber(10) + 35 , Greenfoot.getRandomNumber(600) + 40);
            }
kartikitrak kartikitrak

2012/6/1

#
Hey! You could make a simple counter that only spawns an enemy when the counter reaches 10. So
private int counter = 10;

if (counter == 10)
{
//Spawn 1 enemy
counter = 0;
}
else
{
counter ++
}
You could put this in a for loop also if you wanted to limit the amount of times you want the enemy to spawn. Each level all you gotta do is multiple the number of enemies increased by the level number.
danpost danpost

2012/6/1

#
That is something that would have to be done in the act() method of the world (using a counter initialized to the number of frames before a spawning). You will probably need an int to count the spawns also (initialized to zero). Each act cycle, do the following if the number of spawned actors is less then the total number of spawnings required { decrement the frame counter if frame counter is zero { spawn an actor increment spawn counter reset frame counter } }
You need to login to post a reply.