This site requires JavaScript, please enable it in your browser!
Greenfoot back
leonard.ross@waldenu.edu
leonard.ross@waldenu.edu wrote ...

2011/9/25

How to make an object go up or down?

Hello everyone, I'm a noob at programming and we're working on making objects move, so I need a little help. The question is how can I make an object move north, south, east, west, northwest, etc....By using the keypad? So far I have: if (Greenfoot.isKeyDown("up")) { (// command goes here??) How do I make the object go up using the up arrow on the keypad? Thanks for the help!
ez4u2c ez4u2c

2011/9/25

#
Hi Leonard, I'm also a beginner with Greenfoot. I think that the Greenfoot tutorial (see: http://www.greenfootgallery.org/tutorials ) will get you going. The Wombats scenario walks you through the basics of actor movement rather nicely, so I won't quote any of it here, but it's pretty straightforward. BTW the navigation button labeled "Documentation" on this page takes you right to the tutorial. (Hint: Take a look at the move() method in the Wombat object)
Thanks, I was able to make the object move East & West, trying to make it move North & South is completely different. I still haven't figured that out yet, but I'm going to keep trying. Thanks for your help
mjrb4 mjrb4

2011/9/28

#
Hey, You have the isKeyDown() bit right, in the if statement you'll want something like:
1
setLocation(getX(), getY()+1);
1
setLocation(getX(), getY()-1);
...to move up / down. 1 can be a bigger number for faster speeds.
Duta Duta

2011/9/29

#
Alternatively if you have a move() method, you can try: For going down:
1
2
setRotation(90);
move();
For going up:
1
2
setRotation(270);
move();
For going left:
1
2
setRotation(180);
move();
For going right:
1
2
setRotation(0);
move();
This has some advantages (a major one is if you're wanting to use speeds, or varying speeds), however one problem is that your image (well the image for the actor you are moving) rotates (for obvious reasons). This isn't always a problem, but yeah, just thought I'd suggest that.
Thanks alot!
AGABloggers AGABloggers

2015/5/31

#
Thanks so much!
You need to login to post a reply.