Velocity and the Counter Pattern

You can use a sprite's velocity properties with the counter pattern to change a sprite's velocity during the program. This makes the sprite speed up or slow down.

Speeding Up

To speed up a sprite that has a positive velocity, you need to add to the velocity inside the counter pattern. To speed up a sprite with a negative velocity, you need to subtract from the velocity inside the counter pattern.

Going Up

flybot.velocityY = -1;

function draw() {
  background("black");
  flybot.velocityY = flybot.velocityY - 1;
  drawSprites(); 
}

Going Down

bone.velocityY = 1;

function draw() {
  background("black");
  bone.velocityY = bone.velocityY + 1;
  drawSprites();
}

Going Left

cart.velocityX = -1;

function draw() {
  background("black");
  cart.velocityX = cart.velocityX - 1;
  drawSprites();
}

Going Right

robot.velocityX = 1;

function draw() {
  background("black");
  robot.velocityX = robot.velocityX + 1;
  drawSprites();
}

Slowing Down

To slow down a sprite that has a positive velocity, you need to subtract from the velocity inside the counter pattern. To slow down a sprite with a negative velocity, you need to add to the velocity inside the counter pattern. Once a sprite has slowed down to a stop, it will start speeding in the other direction. This can make it look like your sprite is jumping or has been thrown in the air.

Going Up

bone.velocityY = -25;

function draw() {
  background("black");
  bone.velocityY = bone.velocityY + 1;
  drawSprites();
}

Going Right

cart.velocityX = 25;

function draw() {
  background("black");
  cart.velocityX = cart.velocityX - 1;
  drawSprites();
}

Stopping Your Sprite

If you want your sprite to stop, rather than start to move in the other direction, you'll need to use a conditional to check that the sprite is moving in the correct direction before you use the counter pattern, and stop the sprite if it isn't.

plane.velocityY = 25;

function draw() {
  background("black");
  if (plane.velocityY > 0) {
    plane.velocityY = plane.velocityY - 1;
  }
  drawSprites();
}

Found a bug in the documentation? Let us know at documentation@code.org