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

2016/4/26

Need help!!!!! Quick!

AMiscool AMiscool

2016/4/26

#
Hi, I am in need of help again. I was wondering how to make it so I can have all of my food actors spawn every few seconds randomly somewhere on the screen. (I know there is a code out there where they spawn on the sides, I DO NOT want that one.) Reply the code and where to put it. If there are any more specifics, let me know. Thanks.
danpost danpost

2016/4/26

#
AMiscool wrote...
Hi, I am in need of help again. I was wondering how to make it so I can have all of my food actors spawn every few seconds randomly somewhere on the screen. (I know there is a code out there where they spawn on the sides, I DO NOT want that one.) Reply the code and where to put it. If there are any more specifics, let me know. Thanks.
To spawn anywhere along a side, you make only one of the coordinates random. A random x coordinate with a y of zero or the height of the world would refer to anywhere along the top or bottom of the world. By making both coordinates random, you can end up with any set of coordinates for any point in the world. So, for a set of coordinates for anywhere in the world (x, y):
1
2
3
// coded for the world class
int x = Greenfoot.getRandomNumber(getWidth());
int y = Greenfoot.getRandomNumber(getHeight());
To choose a random food actor, the following can be used:
1
2
3
4
5
6
7
Actor food = null;
switch (Greenfoot.getRanomNumber(3))
{
    case 0:  food = new Grapes(); break;
    case 1:  food = new Apple(); break;
    case 2:  food = new Banana(); break;
}
You can randomize the spawning a bit also with:
1
if (Greenfoot.getRandomNumber(180) == 0) spawnFood();
where the codes above would be in the 'spawnFood' method along with adding the food into the world:
1
addObject(food, x, y);
If you want the food to spawn at regular intervals, add an act counter and when it reaches 180 (instead of the randomly generated value being zero in a 1/180 chance), spawn food and reset the counter back to zero. If you want the food to cycle as far as what spawns, you can add a counter for that an use it in the 'switch' statement instead of the randomly generated value.
AMiscool AMiscool

2016/4/30

#
@danpost could you reply me the full code in one text box so I don't mess up the code? Thanks.
AMiscool AMiscool

2016/4/30

#
danpost wrote...
To spawn anywhere along a side, you make only one of the coordinates random. A random x coordinate with a y of zero or the height of the world would refer to anywhere along the top or bottom of the world. By making both coordinates random, you can end up with any set of coordinates for any point in the world. So, for a set of coordinates for anywhere in the world (x, y): ? 1 2 3 // coded for the world class int x = Greenfoot.getRandomNumber(getWidth()); int y = Greenfoot.getRandomNumber(getHeight()); To choose a random food actor, the following can be used: ? 1 2 3 4 5 6 7 Actor food = null; switch (Greenfoot.getRanomNumber(3)) { case 0: food = new Grapes(); break; case 1: food = new Apple(); break; case 2: food = new Banana(); break; } You can randomize the spawning a bit also with: ? 1 if (Greenfoot.getRandomNumber(180) == 0) spawnFood(); where the codes above would be in the 'spawnFood' method along with adding the food into the world: ? 1 addObject(food, x, y); If you want the food to spawn at regular intervals, add an act counter and when it reaches 180 (instead of the randomly generated value being zero in a 1/180 chance), spawn food and reset the counter back to zero. If you want the food to cycle as far as what spawns, you can add a counter for that an use it in the 'switch' statement instead of the randomly generated value.
Could you Reply me the full code in one text box so I don't mess up the code? Thanks.
Super_Hippo Super_Hippo

2016/4/30

#
The text describes how it should be done. Try it.
CuddlySpartan CuddlySpartan

2016/4/30

#
You just put together the code that danpost gave you. for example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public void randomSpawner()
{
int x = Greenfoot.getRandomNumber(getWidth());
int y = Greenfoot.getRandomNumber(getHeight());
 
Actor food = null;
switch (Greenfoot.getRandomNumber(3))
{
case 0: food = new Grapes(); break;
case 1: food = new Apple(); break;
case 2: food = new Banana(); break;
 
addObject(food,x,y);
}
}
Also if you want the spawning to randomly occur, change "addObject(food,x,y);" to
1
2
3
4
if (Greenfoot.getRandomNumber(100) <= 10)
{
addObject(food,x,y);
}
That code will spawn the food approximately 10% of the time.
danpost danpost

2016/4/30

#
CuddlySpartan wrote...
< Code Omitted > That code will spawn the food approximately 10% of the time.
That would produce about 5 or 6 foods per second -- way to quick when what is wanted is one every 3 to 4 seconds. The first line generating the random number should be more like this:
1
if (Greenfoot.getRandomNumber(360) == 0)
which would average one ever 3 to 4 seconds. At times, however, It would occasionally spawn 2 or 3 food almost instantaneously or maybe a wait of up to 7 or so seconds between spawnings. Better would be to reduce the random range and add a minimum time between spawns. This would require the use of a counter field:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// instance field
private int spawnDelay;
 
// method to reset spawnDelay value
private void resetSpawnDelay()
{
    spawnDelay = 120 + Greenfoot.getRandomNumber(120);
}
 
// for spawning
if (spawnDelay == 0)
{
    randomSpawner();
    resetSpawnDelay();
}
else
{
    spawnDelay--;
}
You need to login to post a reply.