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

2020/5/20

Every time addFood comes up I get cannot find symbol-variable addFood

lova30940 lova30940

2020/5/20

#
Every time addFood comes up I get cannot find symbol-variable addFood I am trying to create a snake game and I am trying to add the food into the world public void populateWorld() { while (addFood < 10) { addObject( new Food(), Greenfoot.getRandomNumber(540), Greenfoot.getRandomNumber(540)); addFood++; } }
Brandman73 Brandman73

2020/5/20

#
did you ever declare the variable before the code?
lova30940 lova30940

2020/5/20

#
Could you give me an example of how that would look?
Brandman73 Brandman73

2020/5/20

#
Add this line before the while loop, int addFood = 0; This addfood variable will only be existing inside the populateWorld() method because it was declared inside the method public void populateWorld() { int addFood = 0; while (addFood < 10) { addObject( new Food(), Greenfoot.getRandomNumber(540), Greenfoot.getRandomNumber(540)); addFood++; } }
lova30940 lova30940

2020/5/20

#
Perfect
You need to login to post a reply.