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

2013/2/5

Need help with some code

JHankins JHankins

2013/2/5

#
How would I program drawDog(int x, int y): Instantiates a Dog object at horizontal location x and vertical location y in the DogWorld? default DogWorld to contain three Dog objects at random locations and one player-controllable Dog object precisely at the center of the DogWorld I have figured out the proper code to get random dogs to appear. I also need a little help in how to add movement with the arrow keys. I am a complete newbie to all of this and would appreciate any help given.
Gevater_Tod4711 Gevater_Tod4711

2013/2/5

#
The method drawDog would look like this:
public void drawDog(int x, int y) {
    getWorld().addObject(new Dog(), x, y);
}
//if this method is in a world class you have to delete the 'getWorld().'
To move your dog using the arrow keys you need the method Greenfoot.isKeyDown(String key). Use it like this:
//in your act method;
if (Greenfoot.isKeyDown("up")) {
    setLocation(getX(), getY()-1);//or a higher value than 1 if you want your dog to move faster;
}
if (Greenfoot.isKeyDown("down")) {
    setLocation(getX(), getY()+1);
}
if (Greenfoot.isKeyDown("left")) {
    setLocation(getX()-1, getY());
}
if (Greenfoot.isKeyDown("right")) {
    setLocation(getX()+1, getY());
}
JHankins JHankins

2013/2/5

#
Thank you so much I appreciate the help. Going to try inserting these now and keep my fingers crossed I do it right.
You need to login to post a reply.