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

2018/10/18

Move actor up and down automatically

Afrobotsigner Afrobotsigner

2018/10/18

#
I need to make my alien spaceship move up and down automatically without having to press any key. The movement needs to be continuous till the ship is shot down. I need to apply the setLocation method. What other code can I use? Thank you in advance Greenfoot community. Regards Afrobotsigners
danpost danpost

2018/10/18

#
Afrobotsigner wrote...
I need to make my alien spaceship move up and down automatically without having to press any key. The movement needs to be continuous till the ship is shot down. I need to apply the setLocation method. What other code can I use?
You will need to track which direction the ship is currently moving. The speed is a constant and can be hard-coded or held in a field. The new y-coordinate of the ship in your setLocation line can then be expressed as 'getY()+dir*speed' or 'getY()+dir*5', for example. The 'dir' field, as an int, would hold a value of '1' or '-1' to add or subtract the speed value. A separate set of code lines will be needed to negate the value of the 'dir' field when conditions are right. The condition could be at specific y-coordinate values or a counter field can be used to limit the number of moves in either direction.
You need to login to post a reply.