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

2014/2/11

I need help moving an object back and forth

mia826 mia826

2014/2/11

#
Basically the object (a card soldier to be specific, like in Alice in Wonderland!) is pacing back and forth between two walls. I've tried using turn(90); and just cross my fingers hoping that the uneven walls (they're individual bushes or trees put side by side) would make it bounce around a bit before settling into sensible pacing. It's not working out though, and the project is due tomorrow, so I don't have time to experiment. (Yes I'm sorry for cramming) It would be much simpler if it was a right to left kind of pacing, but unfortunately what I need is the kind of pacing that goes from the top of the screen to the bottom and back again. I can't use setRotation() to turn the object facing down because then it won't bounce back anymore. Can anyone help me please? :c
danpost danpost

2014/2/11

#
Really the conditions for turning around (or reversing direction, to be more to the point) would be when moving down and reaching some point or when moving up and reaching some other point, reverse direction. Use the 'setLocation' method for moving adjusting 'getY' by 'speed*direction' where 'direction' is one or negative one (just negate its value for changing directions).
You need to login to post a reply.