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

2013/5/22

GAME HELP

FCG FCG

2013/5/22

#
I want fish to generate from the right side of the world then move across to the left and then remove. If they are killed I also want them to generate. Could someone help with a code where the fish will auto generate from the right side when there are less then 15 fish. I already have a code to kill them. In the code can you use a "while" loop.
danpost danpost

2013/5/22

#
You could put the code in the world class act method to add fish if less than 15 are in the world.
if (getObjects(Fish.class).size() < 15) addObject(new Fish(),getWidth()-1, Greenfoot.getRandomNumber(getHeight());
You can put the code to remove the fish in one statement, using methods to return the results of each condition.
if (atWorldEdge() || isEaten() || isKilled()) getWorld().removeObject(this);
Then write the methods that return the boolean value for each condition. For example:
private boolean atWorldEdge()
{
    return getX()==0;
}
This would satisfy the first condition, provided you are removing the fish only at the left edge of the world.
FCG FCG

2013/5/22

#
danpost wrote...
You could put the code in the world class act method to add fish if less than 15 are in the world.
if (getObjects(Fish.class).size() < 15) addObject(new Fish(),getWidth()-1, Greenfoot.getRandomNumber(getHeight());
You can put the code to remove the fish in one statement, using methods to return the results of each condition.
if (atWorldEdge() || isEaten() || isKilled()) getWorld().removeObject(this);
Then write the methods that return the boolean value for each condition. For example:
private boolean atWorldEdge()
{
    return getX()==0;
}
This would satisfy the first condition, provided you are removing the fish only at the left edge of the world.
Is there a way to do this using the "while" or "for"? My teacher wants us to use that.
danpost danpost

2013/5/23

#
I guess you could use a 'while' with the same condition to add fish into the world.
FCG FCG

2013/5/23

#
danpost wrote...
I guess you could use a 'while' with the same condition to add fish into the world.
Could you post the code, I'm not that good.
danpost danpost

2013/5/23

#
danpost wrote...
You could put the code in the world class act method to add fish if less than 15 are in the world.
if (getObjects(Fish.class).size() < 15) addObject(new Fish(),getWidth()-1, Greenfoot.getRandomNumber(getHeight());
The above is what I had originally said. Just change 'if' to 'while'. This will cause any fish needed to be added to the world to make a total of fifteen happen all in one act cycle, instead of just one per act cycle.
FCG FCG

2013/5/25

#
danpost wrote...
You could put the code in the world class act method to add fish if less than 15 are in the world.
if (getObjects(Fish.class).size() < 15) addObject(new Fish(),getWidth()-1, Greenfoot.getRandomNumber(getHeight());
You can put the code to remove the fish in one statement, using methods to return the results of each condition.
if (atWorldEdge() || isEaten() || isKilled()) getWorld().removeObject(this);
Then write the methods that return the boolean value for each condition. For example:
private boolean atWorldEdge()
{
    return getX()==0;
}
This would satisfy the first condition, provided you are removing the fish only at the left edge of the world.
Is there a way of checking how many fish are set in the background already before the level starts because it doesn't take that into account. when setting new fish
danpost danpost

2013/5/25

#
FCG wrote...
Is there a way of checking how many fish are set in the background already before the level starts because it doesn't take that into account. when setting new fish
What do you mean by 'set in the background'? Do you mean that they are drawn onto the image of the background and are not Actor objects? if so, do you draw them on programmatically, or are they already on the image?
FCG FCG

2013/5/25

#
danpost wrote...
You could put the code in the world class act method to add fish if less than 15 are in the world.
if (getObjects(Fish.class).size() < 15) addObject(new Fish(),getWidth()-1, Greenfoot.getRandomNumber(getHeight());
You can put the code to remove the fish in one statement, using methods to return the results of each condition.
if (atWorldEdge() || isEaten() || isKilled()) getWorld().removeObject(this);
Then write the methods that return the boolean value for each condition. For example:
private boolean atWorldEdge()
{
    return getX()==0;
}
This would satisfy the first condition, provided you are removing the fish only at the left edge of the world.
This code didn't work. When the enemy is removed they don't regenerate.
FCG FCG

2013/5/25

#
danpost wrote...
You could put the code in the world class act method to add fish if less than 15 are in the world.
if (getObjects(Fish.class).size() < 15) addObject(new Fish(),getWidth()-1, Greenfoot.getRandomNumber(getHeight());
You can put the code to remove the fish in one statement, using methods to return the results of each condition.
if (atWorldEdge() || isEaten() || isKilled()) getWorld().removeObject(this);
Then write the methods that return the boolean value for each condition. For example:
private boolean atWorldEdge()
{
    return getX()==0;
}
This would satisfy the first condition, provided you are removing the fish only at the left edge of the world.
Do you know why the fish don't regenerate once they are removed.
danpost danpost

2013/5/25

#
I cannot say without seeing what code you have now. It might be because you placed the code incorrectly within the class; or any one of many other reasons.
You need to login to post a reply.