Velocity (not to be confused with gravity) is defined as speed in a given direction, and can therefore be negative, unlike speed alone (which is directionless and always positive). It is often used in 專案 for physics effects. Using velocity is a far smoother and aesthetically pleasing method than traditional scrolling.
How to program velocity in Scratch
This is a common method used to program velocity:
重複無限次 如果 <[向左 v] 鍵被按下？> 那麼 變數 [Speed v] 改變 (-1) // a negative speed for leftward motion end 如果 <[向右 v] 鍵被按下？> 那麼 變數 [Speed v] 改變 (1) // a positive speed for rightward motion end 變數 [Speed v] 設為 ((Speed) * (0.9)) // a larger number increases the max speed and acceleration x 改變 (Speed) // the actual motion of the sprite end
The above technique is simple, efficient and contained within a single 程式. The numbers can be changed to affect how quickly the sprite can speed up and slow down. It should be noted that the number in the last set 變量 積木 must be under 1, or the sprite will speed up rather than slowing down gradually.
Velocity has many uses — it can be used almost anywhere. A few of the many uses are below:
- Racing and driving games
— Vehicles with velocity are more realistic to drive with.
— Velocity makes the movement more realistic.
- Physics simulations
— For example, a bouncing ball can use this.
- Scrolling projects
— Scrollers with velocity are popular. In general, anywhere that a sprite is moving — simple on-off movement — can easily be enhanced with velocity.
Below are some projects that use velocity:
- Velocity engine
- Velocity Tutorial
- Super Smash Bros. Ball!
- Off-Road Rally
- Ice-olated V 1.0
- Velocity Test