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

2012/6/29

Skipping a line?

1
2
danpost danpost

2012/6/29

#
If you are asking: is it possible with the code I provided, no. You will always exit the first 'if' block with just one target in the world. However, you could repeat the code in the first code block to produce two at essentially the same time. What will happen from there, is the sniper will have to destroy both targets before two more targets are created. The code to create the second target should not have the 'int' castings on 'spawnX' and 'spawnY', as they will already be declared as such.
danpost danpost

2012/6/29

#
You could create a method to spawn targets:
private void spawnTarget()
{
    Target target = new Target();
    int spawnX = Greenfoot.getRandomNumber(getWidth());
    int spawnY = Greenfoot.getRandomNumber(getHeight());
    addObject(target, spawnX, spawnY);
}
Declare an int field in the world class to track how many targets are to be spawned at a time:
private int numberOfTargetsToSpawn = 1;
Then, in the act method, your first 'if' block could be
if (getObjects(Target.class).isEmpty())
{
    for (int i = 0; i < numberOfTargetsToSpawn; i++) spawnTarget();
}
Now, you can increase the value of 'numberOfTargetsToSpawn' as you want. That many targets will be created, the sniper will destroy all of them, and that many more will then be created, etc.
erdelf erdelf

2012/6/29

#
thx again
You need to login to post a reply.
1
2