Hi
I need to program a game for my end work for school, I've the basic concept in mind of what I need to program and how I need to do it.
I've tried a not of things but I can't find the problem. One of the things I tried is a little messy.
First, I know my code that I've written can be better, more compact. And my var naming is different. If you know the solution to my
problem can you please just write it in text with No example of code. Just point out the things I need to change. Thank you.
So the problem:
I need to spawn at a given interval 1 enemy or ally, they will spawn in an 75 pixel area (X randomly), and at a random Y. This part of the
spawning works, the trouble I'm having now is instead of spawning one enemy or Ally, it spawns 30+ (way to much).
This part of the program runs on a clock for each 100 acts Greenfoot does, 1 'second' will be counted. then the messy thing I've written
does: for every second another var will go to 1 (I also could have used a boolean), at the end of the randomizer this will be set back to
zero. this var is supposed to let the randomizer algorithm go only once a second.
There is another var called tSC which counts how manytimes the randomizer algorithm has been run. this one also lets the program
decide which tear (the first 3 if's in the randomizer) of difficulty to pick and which enemy or ally it will be.
The 3 following if's are the intervals an enemy or ally can spawn and it decides randomly where to spawn that enemy or ally.
The last IF is when the algorithm has ended it's lap (6 seconds) and the second counter will be set to 0, the tSC var will be added by one.
thank you for pointing out the things i need to change to make the randomizer work.
public class MyWorld extends World
{
private int j = 0;
private int tS = 0;
private int tSC = 0;
private int aS1 = 0;
private int aS2 = 0;
private int aS3 = 0;
private int tF1 = 0;
/**
* Constructor for objects of class MyWorld.
*
*/
public MyWorld()
{
super(750, 349, 1);
addObject(new Cannon(), 40, 175);
}
public void act()
{
/*
* Randomizer for Enemy and Ally spawn in world
* Chooses randomly an enemy type or ally and a Y coordinate to spawn it
* The X coordinate has a 75 pixel area to spawn and all enemies and allies move in the same direction
* One algorithm lap takes 6 seconds: One spawn each 2 seconds (not in the beginning)
* In the beginning easy enemies and allies will be spawned after … times the spawn algorithm has run more difficult enemies will spawn
* After … times an enemy or ally has spawned more will spawn
* This part only handles the spawning of enemies and allies
*/
// Clock to convert acts/second into 1 second
j = j + 1;
if ( j == 100 )
{
tS = tS + 1;
j = 0;
}
// spawn algorithm can only fire once a lap (one lap = tS++)
if (tS == 1)
{
tF1 = 1;
}
if (tS == 2)
{
tF1 = 1;
}
if (tS == 3)
{
tF1 = 1;
}
if (tS == 4)
{
tF1 = 1;
}
if (tS == 5)
{
tF1 = 1;
}
if (tS == 6)
{
tF1 = 1;
}
// spawn algorithm (randomizer)
if ( tF1 == 1 )
{
if (tSC >= 0 && tSC <= 8)
{
aS1 = Greenfoot.getRandomNumber(2);
aS2 = Greenfoot.getRandomNumber(2);
}
if (tSC >= 8 && tSC <= 14)
{
aS1 = Greenfoot.getRandomNumber(2);
aS2 = Greenfoot.getRandomNumber(3);
aS3 = Greenfoot.getRandomNumber(3);
}
if (tSC >= 14)
{
aS1 = Greenfoot.getRandomNumber(4);
aS2 = Greenfoot.getRandomNumber(4);
aS3 = Greenfoot.getRandomNumber(4);
}
if (tS == 1)
{
if (aS1 == 0)
{
addObject(new Ally(), Greenfoot.getRandomNumber(76) + 650, Greenfoot.getRandomNumber(300) + 25);
}
if (aS1 == 1)
{
addObject(new Enemy(), Greenfoot.getRandomNumber(76) + 650, Greenfoot.getRandomNumber(300) + 25);
}
if (aS1 == 2)
{
addObject(new EnemyF(), Greenfoot.getRandomNumber(76) + 650, Greenfoot.getRandomNumber(300) + 25);
}
if (aS1 == 3)
{
addObject(new EnemyH(), Greenfoot.getRandomNumber(76) + 650, Greenfoot.getRandomNumber(300) + 25);
}
}
if (tS == 3)
{
if (aS2 == 0)
{
addObject(new Ally(), Greenfoot.getRandomNumber(76) + 650, Greenfoot.getRandomNumber(300) + 25);
}
if (aS2 == 1)
{
addObject(new Enemy(), Greenfoot.getRandomNumber(76) + 650, Greenfoot.getRandomNumber(300) + 25);
}
if (aS2 == 2)
{
addObject(new EnemyF(), Greenfoot.getRandomNumber(76) + 650, Greenfoot.getRandomNumber(300) + 25);
}
if (aS2 == 3)
{
addObject(new EnemyH(), Greenfoot.getRandomNumber(76) + 650, Greenfoot.getRandomNumber(300) + 25);
}
}
if (tS == 5)
{
if (aS3 == 0)
{
addObject(new Ally(), Greenfoot.getRandomNumber(76) + 650, Greenfoot.getRandomNumber(300) + 25);
}
if (aS3 == 1)
{
addObject(new Enemy(), Greenfoot.getRandomNumber(76) + 650, Greenfoot.getRandomNumber(300) + 25);
}
if (aS3 == 2)
{
addObject(new EnemyF(), Greenfoot.getRandomNumber(76) + 650, Greenfoot.getRandomNumber(300) + 25);
}
if (aS3 == 3)
{
addObject(new EnemyH(), Greenfoot.getRandomNumber(76) + 650, Greenfoot.getRandomNumber(300) + 25);
}
}
if (tS == 6)
{
tS = 0;
tSC = tSC + 1;
}
tF1 = 0;
}
}
}

