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

2020/5/20

I am trying to add make it so that food keeps spawning every time the snake eats it.

lova30940 lova30940

2020/5/20

#
In the code addObject it keeps saying that method addObject in class Greenfoot.world cannot be applied to the given types public class MyWorld extends World { /** * Constructor for objects of class MyWorld. * */ Player bluePlayer = new Player(0,255); Player whitePlayer = new Player(255,0); Counter blueCounter = new Counter(); Counter whiteCounter = new Counter(); int count=0; public MyWorld() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(600, 400, 1); getBackground().setColor(Color.BLACK); getBackground().fill(); addObject(bluePlayer, 450, 300); addObject(whitePlayer, 450,100); addObject(blueCounter, 550,50); addObject(whiteCounter, 50, 50); } public void act() { count++; if(count >100) addObject(new Food(255,0, 0)); Greenfoot.getRandomNumber(getWidth() -1); Greenfoot.getRandomNumber(getHeight() -1); } }
Yehuda Yehuda

2020/5/20

#
Your last addObject is missing the x and y parameters. That is, it's missing the two numbers that tell where the object should be placed. Also, the two lines below aren't being used for anything as of now. Perhaps you wanted the addObject to be after, and then use the random nuber for the x and y.
lova30940 lova30940

2020/5/20

#
@Yehuda do you have an example?
Yehuda Yehuda

2020/5/20

#
Example of what? We're looking at your code now. What's going on in the act method, and what's it supposed to do?
danpost danpost

2020/5/20

#
You say the new food is to spawn when old food is eaten. Why then is count even there in your code -- where you add food when it is increased to over 100? That is not the condition you say you want.
lova30940 lova30940

2020/5/20

#
Can you guys give me an example of how the code would look if it were right?
danpost danpost

2020/5/20

#
lova30940 wrote...
Can you guys give me an example of how the code would look if it were right?
This post describes your problem as well (very similar ideas with different details).
You need to login to post a reply.