Workshop › Pinball › Step 28 of 42
Each frame a flipper picks a target (up if its key is held, rest otherwise) and a turning speed towards it:
FLIP_SPEED a frame, or just the rest of the way when it is nearly there.
```js
const FLIP_SPEED = 0.25 // radians per frame
for (const f of flippers) {
// Up while the key is held, back down when it is let go, and stop at either end.
const target = pressed[f.key] ? f.up : f.rest
const dir = Math.sign(target - f.angle)
f.speed = Math.abs(target - f.angle) < FLIP_SPEED ? (target - f.angle) : dir * FLIP_SPEED
}
```pressed[f.key] reads the field named by f.key: pressed.left for the left flipper.Math.sign gives 1, -1 or 0: which way to turn. Math.abs drops the minus: how far there is to go.dir.dir instead of assuming "up is smaller" makes the same code right for the right flipper, whose up angle is
the bigger one.FLIPPER_LENGTH write FLIP_SPEED.update, under the flash = ... line, write the for loop.Skills: Velocity and gravity, Game states
← Which keys are held? Flip! →