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

2015/3/3

Hey guys, I'm a newbie to Greenfoot, teaching it to Y8 class down here in New Zealand. Hoping someone could help me...

johansj johansj

2015/3/3

#
I'm teaching my class the basics in Java through the use of Greenfoot, which is awesome by the way. I'm having a problem adding code to World > Ground - I don't know the right place in the editor to add the code. Here are the instructions we are following... (see link below) What do we want to do with Snake? We want it to move to the Wombat. We’re going to do that by using a new function of Greenfoot 2.2, which is turnTowards(int x, int y). But for that method, we need the coordinates of the Wombat. We are going to store that in the Ground class. Add this to the Ground: private double userX; private double userY; Give them a value in the action Ground(): userX = user.getExactX(); userY = user.getExactY(); And add the act() method to Ground: public void act() { if (user != null) { userX = user.getExactX(); userY = user.getExactY(); } } Now, the userX and userY doubles will automatically be updated, except for when there’s no Wombat in the world. We use doubles instead of integers, because we imported the class SmoothMover which is meant to use doubles as location. Snake Avoider instructions Is there anyone that could show me how/where this code goes in the World editor? Any help, support would be greatly appreciated. Cheers Joe
lordhershey lordhershey

2015/3/3

#
An actor can sense another one by using the Actors getObjectInRange( Class ) method, or the world's getObjects( Class ), the world can be obtained by an actor in it by calling getWorld(), so it would looks like getWorld().getObjects( wombat.class ) and you will get a list of all wombats in the world. Then just query the wombat instance for its getX() and getY(), turn towards that x,y. Or something like that.
danpost danpost

2015/3/3

#
@Joe, what you are showing above is a perfect example of using fields to hold values that are already being held in fields somewhere else. 'exactX' and 'exactY' are inherited by the Wombat instance from the SmoothMover class; so, those values are accessible from there. There is no need to save them again elsewhere. And, although these values are saved as doubles, the 'turnToward' method requires int values; so, 'getX' and 'getY' should suffice. What you could use in your Ground class is a field for the Wombat instance itself and a getter method for that field. That would be something that does not need continuous value changing and also make the real-time coordinates of the wombat readily accessible.
// instance field
private Actor wombat; // or 'private Wombat wombat;'
// getter method
public Actor getWombat() // or 'public Wombat getWombat()'
{
    return wombat;
}
Then your snake can use:
Actor wombat = ((Ground)getWorld()).getWombat();
if (wombat != null && wombat.getWorld() != null) turnTowards(wombat.getX(), wombat.getY());
You may or may not need one or both of the conditions in the 'if' statement. ******************** You could, alternatively, get away without any fields in the Ground class and use 'getWorld().getObjects(Wombat.class)', like lordhershey suggests, to get a list of all wombats in the world. You would then, however, need to make sure the list is not empty, then get the first element in the list (which would then be the one wombat in the world), followed by casting the object as an Actor (or a Wombat) so you can execute 'getX" and 'getY' on it.
davmac davmac

2015/3/4

#
Joe, the basic structure of classes is explained here: http://docs.oracle.com/javase/tutorial/java/javaOO/classes.html Your "private double userX" and "private double userY" are fields so they go near the top of the class. This:
Give them a value in the action Ground():
userX = user.getExactX();
userY = user.getExactY();
... possibly means the constructor Ground(), although I'm not certain that it's really necessary to include those lines, since their effect is duplicated in the subsequent instructions:
And add the act() method to Ground:
public void act() {
    if (user != null)
    {
        userX = user.getExactX();
        userY = user.getExactY();
    }
}
'public void act() { ... }' declares a method, see the class structure that I linked above for where to put it. Note that we can't see the instructions you linked to, since you linked to a file on your own computer or network, which we don't have access to. If you want to link to a file here, you need to make sure it is public accessible via a 'http' URL.
johansj johansj

2015/3/10

#
Thanks guys, I will try that out and see how we go. Cheers again J
You need to login to post a reply.