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

2020/4/10

Increasing speed over time

NewUser1201 NewUser1201

2020/4/10

#
I am very new to coding and Greenfoot, so go easy on me. For part of my assignment, I need to have my enemy's speed increase over time, however, I'm not sure how you do this, and everything I try just breaks the game, here is the base code that makes the enemy move, if you can figure out how to increase the enemy's speed by 1 every 15 seconds or so, that would be greatly appreciated public class Enemy extends Actor { int X_MOVE = 3; int Y_MOVE = 3; public void act() { setLocation(getX() + X_MOVE, getY() + Y_MOVE); if ((getX() > 580) || (getX() < 20)) { X_MOVE = X_MOVE * -1; } else if ((getY() > 380) || (getY() < 20)) { Y_MOVE = Y_MOVE * -1; } } }
danpost danpost

2020/4/10

#
You will need a field for the timer. Have it count act steps. Usually, about 60 steps will happen within one second; so, when the timer hits 900 (15 seconds -- about) reset it to zero and bump up the move amounts. Only problem, then, is that the direction is given in the X_MOVE and Y_MOVE values; so, adding one could potentially slow the enemy down. Bump the speed using the following:
X_MOVE += Math.signum(X_MOVE);
Y_MOVE += Math.signum(Y_MOVE);
NewUser1201 NewUser1201

2020/4/10

#
Thank you for your help @danpost, ngl took me a few tries to read and fully understand what you were saying but I figured it out, again thanks!
You need to login to post a reply.