Moving a sprite around the screen
Now that we have the George sprite animating, we will build on that and change the animation based on the direction of travel:
- Let's add some variables at the top of the
George
class to store the animations and direction information:late SpriteAnimation georgeDownAnimation, georgeLeftAnimation, georgeUpAnimation, georgeRightAnimation; double elapsedTime = 0.0; double georgeSpeed = 40.0; int currentDirection = down; static const int down = 0, left = 1, up = 2, right = 3;
- Below the
onLoad
function, define a new function calledchangeDirection
, which we will call every 3 seconds to change George's direction randomly:void changeDirection() { Random random = Random(); int newDirection = random.nextInt(4); switch (newDirection...