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

2013/1/27

Write a method.

avocasso avocasso

2013/1/27

#
I am new to this programming world. Can someone help me please? I am trying to Write a method (and call the method) within the appropriate class that allows you to control the Pig object in the center of PigWorld according to the following control scheme. Assume that the top of the screen represents north, the right side of the screen represents east, the bottom of the screen represents south, and the left side of the screen represents west: a. Up arrow: Move north b. Down arrow: Move south c. Right arrow: Move east d. Left arrow: Move west e. Up + right arrow pressed simultaneously: Move northeast f. Up + left arrow pressed simultaneously: Move northwest g. Down + right arrow pressed simultaneously: Move southeast h. Down + left arrow pressed simultaneously: Move southwest
Gevater_Tod4711 Gevater_Tod4711

2013/1/27

#
If you just want your pig to move like this it's easy:
//write this in your act method;
if (Greenfoot.isKeyDown("up")) {
    setLocation(getX(), getY() - 1);//or a higher value than 1 if you want your pig 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());
}
avocasso avocasso

2013/1/27

#
Hi Gevater..., Thank you for your assistance, one thing I noticed using your codes is that the pig slides to any direction I control it to go to. Now if I want to control the pig in the same way with walking motion. what is your thought on that. This is what I created so far please let know your thought: /** * Check whether a control key on the keyboard has been pressed. * If it has, react accordingly. */ public void walkMyPig() { if (Greenfoot.isKeyDown("up")) { walkUp(2); } if (Greenfoot.isKeyDown("down")) { walkDown(2); } if (Greenfoot.isKeyDown("right")) { walkRight(2); } if (Greenfoot.isKeyDown("left")) { walkLeft(2); } } }
moobe moobe

2013/1/27

#
change
walkLeft(2);

walkRight(2);
to
setRotation(-2):

setRotation(2);
I think this is what you were asking for ;)
avocasso avocasso

2013/1/28

#
Thanks, Moobe. I have another concern, please help me if you can, about ' CORRECT THIS ERROR FOR ME ' that is a new post. Thanks
You need to login to post a reply.