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

2012/8/17

Maximum objects

havoc131 havoc131

2012/8/17

#
hey, Im new in greenfoot and Im at the crab. I hope someone of you can help me^^ ok, here is my problem: if ( canSee(Spider.class) ) if ( canSee(Lobster.class) ) { eat(Lobster.class); Greenfoot.playSound("crunch.mp3"); getWorld().addObject(new Lobster(), 36,360); getWorld().addObject(new Lobster(), 36,360); } it works great but after a while there are too many lobsters, so can I do it like that there are spawned up to like 5 lobster? so that there is only one spawned then there is one eaten? and another thing I want to change is, that I have two players. the games end, when they have eaten 10 worms. And after one of them has eaten 10 I want to make it harder. the code looks like this: public void act() { if(Greenfoot.getRandomNumber(100) < 3) { addObject(new Worm(), Greenfoot.getRandomNumber(560),Greenfoot.getRandomNumber(560)); } } i want that, after the first "level" the 3 rise like up to 10 or 20. is it possible to make something like that?
Zamoht Zamoht

2012/8/17

#
I think the first problem can be solved by removing one of the "getWorld().addObject(new Lobster(), 36,360);" lines. Or you can make a list of lobsters, by importing List: "import java.util.List;" Then to make the list use this line: "List lobsters = getWorld().getObjects(Lobster.class);" And then add this line to your code: if ( canSee(Spider.class) ) if ( canSee(Lobster.class) ) { eat(Lobster.class); Greenfoot.playSound("crunch.mp3"); -------->if (lobsters.size() <= 5) { getWorld().addObject(new Lobster(), 36,360); } } Without the arrow thing of course. And I'm too tires to look at the next problem right now I'm sorry, though please tell me if I didn't understand your first problem right.
havoc131 havoc131

2012/8/18

#
yes, thanks for your help. thats what i wanted.
Zamoht Zamoht

2012/8/18

#
For your next problem you could make a static int for your "Player" class: "static int lobstersEaten;" Then add "lobstersEaten++;" to your eat method. Now you could have an integer in your world class, which is set to 3 from the start: "public int something = 3". Here public is important because you want to change the something variable from another class. So in your "Player" class act method add something like:
if (lobstersEaten >= 10)
{
      NameOfYourWorld myWorld = ((NameOfYourWorld) getWorld());
      myWorld.something = 10;
}
And change "if(Greenfoot.getRandomNumber(100) < 3)" to "if(Greenfoot.getRandomNumber(100) < something)" Okay so I would like to add some comments to this. First of all I was a bit confused if it was worms or lobsters to be eaten, but you can just change the names in my code. Secondly the something integer variable should be named something else. And last thing is that NameOfYourWorld should be the name of your world class. This should do it, but please tell me if this just gave you a lot of errors.
You need to login to post a reply.