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

2013/9/5

Help with multiple collisions and eating!

lonesoldierx7 lonesoldierx7

2013/9/5

#
Hey guys, I'm trying to make a basic game where when my turtle collides with a bug multiple times,it will give me a score of eating 5 lettuces. I'm pretty sure I have to start with the canSee method but I do not know how to make it so that once the turtle 'sees' the bug multiple times, it will get eaten. The last thing is how can I combine my point system? The game ends when I eat 10 lettuces. When I eat the bug, it'll give me 5 points. How can I combine the point system?
RUMMAKER RUMMAKER

2013/9/6

#
not sure what you mean by combine the point system? how much is a lettuce worth in points?
lonesoldierx7 lonesoldierx7

2013/9/6

#
RUMMAKER wrote...
not sure what you mean by combine the point system? how much is a lettuce worth in points?
i made it so that when there are a certain amount of objects left, the game ends. Is there a way to create a point system where each lettuce is 1 point and the bug is 5?
RUMMAKER RUMMAKER

2013/9/6

#
try this public class Example extends World { public int points; public Example() { super(19, 19, 30); points = 0; } public void addPoints(int p) { points += p; } } in the actor class public class Lettuce extends Actor { public void act() { // Add your action code here. } public void eaten() { World w = (Example)getWorld(); //do this so greenfoot knows what world you are looking for w.addPoints(1); } }
danpost danpost

2013/9/6

#
You could do this:
// add these instance fields
private int bugsAdded, lettuceAdded; // holds the count of objects added to world
// last thing in your world constructor
bugsAdded = getObjects(Bug.class).size(); // initialize the bug count
lettuceAdded = getObjects(Lettuce.class).size(); // initialize the lettuce count
// in act method
if (lettuceAdded-getObjects(Lettuce.class).size()+(bugsAdded-getObjects(Bug.class).size())*5 >=10) Greenfoot.stop();
If you add more lettuce or bugs to the world after the initial world construction, you will need to adjust the field value(s). With this way, you do not need to keep a running score for the turtle.
You need to login to post a reply.