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

2014/5/18

Moving object up and down

haz9999 haz9999

2014/5/18

#
hi guys I'm very very new and nooby at green foot. I'm trying to create a simple game and i simply want a rock to move up, down, up, down …. etc. Is there any tutorials or code that anyone can show me to help me achieve this? as i can't seem to find anything on the internet. Thanks for the help p.s ( I haven't started the coding yet, I'm wanting to learn how to do this)
danpost danpost

2014/5/18

#
You just need a direction field that alternates between +1 and -1 that gives the speed value direction when multiplied by it.. You will need checks in your act method (or a method it calls) for the conditions required to changed direction and when the conditions are met, multiply the direction field by -1 (which changes it to the other number; -1 to 1 and 1 to -1). The conditions for changing directions at the top or the bottom are two parters. For example, if the direction is currently up and the upper limit is reached, change directions; and similar for the lower limit. Actually, you do need the direction field, as you can just multiply the speed itself by -1 when the conditions are met. Still, however, the speed will have to be kept in a field.
You need to login to post a reply.