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

2014/1/22

make an object move up and down?

0868626 0868626

2014/1/22

#
Hello i have a question about my game. i have an object and i want to move it to start moving 5 steps up and then down again infinitly, im just a beginner so sorry if i sound stupid..
lordhershey lordhershey

2014/1/22

#
I would do something like this: int counter = -1; //member variable public void act() { counter = (counter +1) % 10; if(counter < 5) { setY(getY() + 5); } else { setY(getY() - 5); } } Something like that. the counter will go from 0 to 9 forever, if the counter is go up and when it is it goes back down. You can make it start at a random part in the wave by doing this: int counter = Greenfoot.getRandomNumber(10); the +5 and -5 are just numbers I pulled out of thin air; it can be whatever you need it to be.
You need to login to post a reply.