Download the code for Movement
Drag and release a sprite and it continues to move based on the dragging velocity.
DragThrow is an update to the DragDrop behavior. Instead of dragging and dropping the sprite on the screen, we drag and release the sprite and it continues to move at its dragged velocity. Add the WrapAround behavior to the sprite and it wraps around to the opposite side of the screen after it exits the screen.
WrapAround takes two arguments canvasW (the canvas' width) and canvasH (the canvas' height). WrapAround uses the values of canvasW and canvasH to determine when the sprite exits that screen and to reposition the sprite on the opposite side of the screen.
You can pass DragThrow an argument of friction which determines how quickly the sprite's velocity decreases after it's dragged and thrown. The default friction amount is 0.95. Keep friction's value within a range of 0 to 1.0.
The higher the friction value, the slower the decrease in velocity. This is the result of the friction calculation which multiplies friction by the sprite's velocity.
If our velocity is 10 pixels and we multiply it by a friction value of 0.8, the new velocity would be 8 pixels; a twenty percent drop in velocity. If the friction is 0.99, our 10 pixel velocity drops just a little to 9.9 pixels.
Create your own drag-and-throw project by updating the setup script, js/projects/movement/drag-throw-setup.js.