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

2015/3/17

Greenfoot Help THANKS!

werdnail werdnail

2015/3/17

#
Hi all! :) I need some help, i know this may sound simple to you all but i am stuck and require some assistance. So i have my program. And i need my bug to drop a leaf once every lap in the spots i have put arrows to in the picture below. I need to make a loop that counts the amount of laps and places a leaf in each corner per lap, 4 laps in total and i am stuck. This is the code i have so far which allow my bug to walk around the desired path consistently in a loop but i need help with the code to make the bug drop leafs in each corner per lap please help thanks! MY CURRENT CODE:: public void run() { if(!treeFront()) move(); if(treeFront()) turnLeft(); } } SCREENSHOT HERE
danpost danpost

2015/3/17

#
Your bug will be at a corner when it turns. So, add a leaf at that time. That is also when each lap will end; so, you can also increment your counter at that time.
werdnail werdnail

2015/3/18

#
Yes i understand thank you, but could you please write the code for me so that i can fully undestand it. Thanks
danpost danpost

2015/3/18

#
In your 'run' method, the last part should be something like this:
if (treeFront())
{
    turnLeft();
    addLeaf();
    recordLap();
}
Of course, 'addLeaf' and 'recordLap' will need to be adjusted (as I gave it just as an example of what it would look like).
You need to login to post a reply.