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

2017/5/21

Leaves and Wombats

anu1217 anu1217

2017/5/21

#
I'm working with Leaves and Wombats and I'm trying to add a turnRight method to make the wombats move differently from how they are initially programmed to move. However, I see there is a public void turnRight() method already in the code when I first open up the program. How would I add/modify a turnRight method in this case?
danpost danpost

2017/5/21

#
Modifying the 'turnRight' method will change what the method does (which you probably do not want to do). However, if you want the wombats to move differently, then you need to modify the 'act' method (which may or may not call the 'turnRight' method; and which may or may not only call it upon certain conditions -- by using the 'if' statement, for example).
anu1217 anu1217

2017/5/21

#
How would I modify the act method? This is what I currently have in the act method.
public void act()
    {
        if (foundLeaf()) {
            eatLeaf();
        }
        else if (canMove()) {
            move();
            turnRight();
        }
        else {
            turnRight();
        }
    }
danpost danpost

2017/5/21

#
anu1217 wrote...
How would I modify the act method? This is what I currently have in the act method.
That depends on what behavior you wish to give the wombat. Currently the wombat will either eat a leaf or it will both turn and if can move, it will move also. What would you rather it do?
anu1217 anu1217

2017/5/21

#
I want to change the angle it moves in. I found that this can be done in public void turnRight() below the act method.
Yehuda Yehuda

2017/5/21

#
If in the turnRight method there is turn(90), then to make it turn left instead you can make it be turn(-90). If you don't say how it's turning right now, and how you want it to be then it will be nearly impossible to help.
anu1217 anu1217

2017/5/21

#
Yes, it is at turn(90) now. I changed it to turn(61) to make it move differently.
Yehuda Yehuda

2017/5/21

#
anu1217 wrote...
Yes, it is at turn(90) now. I changed it to turn(61) to make it move differently.
So is this discussion over, or do you still have a problem.
anu1217 anu1217

2017/5/21

#
Yes, I found out how to change the way the wombats move. That was what I was looking for.
You need to login to post a reply.