When a game checks for a spacebar press on every rendered frame, firing a rocket on every check is too fast. Throttling wraps a function and returns a throttled function, limiting how often that action runs. section 29.3
next() {
super.next(); 1
if (isPressingSpace()) { 2
this.fire(); 3
}
return this; 4
}
!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":1,\"ch\":36},{\"line\":1,\"ch\":37}],[{\"line\":3,\"ch\":36},{\"line\":3,\"ch\":37}],[{\"line\":4,\"ch\":36},{\"line\":4,\"ch\":37}],[{\"line\":7,\"ch\":36},{\"line\":7,\"ch\":37}]]"}
!@%STYLE%@!
The update hook checks for the spacebar, calls fire when pressed, and returns the ship. section 29.3
export default class Ship extends ControllableSprite {
static shape = shape;
static stroke = '#fff';
fire = throttle(() => { 1
const x = getSpriteProperty(this, 'x');
const y = getSpriteProperty(this, 'y');
const rotation = getSpriteProperty(this, 'rotation');
new Rocket({ x, y, rotation });
});
...
}
!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":4,\"ch\":62},{\"line\":4,\"ch\":63}]]"}
!@%STYLE%@!
The ship’s fire property holds a throttled arrow function that creates a rocket. Because it is an instance property rather than a prototype method, subclasses cannot override it with super.fire. section 29.3













