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.
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.
flybot.velocityY = -1; function draw() { background("black"); flybot.velocityY = flybot.velocityY - 1; drawSprites(); }
bone.velocityY = 1; function draw() { background("black"); bone.velocityY = bone.velocityY + 1; drawSprites(); }
cart.velocityX = -1; function draw() { background("black"); cart.velocityX = cart.velocityX - 1; drawSprites(); }
robot.velocityX = 1; function draw() { background("black"); robot.velocityX = robot.velocityX + 1; drawSprites(); }
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.
bone.velocityY = -25; function draw() { background("black"); bone.velocityY = bone.velocityY + 1; drawSprites(); }
cart.velocityX = 25; function draw() { background("black"); cart.velocityX = cart.velocityX - 1; drawSprites(); }
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