liveBook · Manning

isPressingSpace

As explained in Get Programming with JavaScript Next: New features of ECMAScript 2015, 2016, and beyond

A game-framework helper function that reports whether the player is pressing the spacebar.

isPressingSpace() reports whether the player is pressing the spacebar. Check it in a ship’s next() hook to decide whether to fire; throttling the firing function slows repeated shots across fast-rendering frames. 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 ship checks the helper during each frame update and calls fire() when the spacebar is pressed. section 29.3

How Get Programming with JavaScript Next: New features of ECMAScript 2015, 2016, and beyond covers it

How deep it goes
Defines the input check and shows it in a frame-update hook, without detailing the helper’s implementation.
Examples
Uses a ship to fire rockets from its position and direction when spacebar input is detected.
What you can do after
Apply the helper in a sprite’s next() hook to trigger rocket firing, using throttling to limit repeated shots.
How it connects
Connects spacebar input to sprite next() hooks and throttled firing.

This explainer was generated by Manning's knowledge graph from the text of Get Programming with JavaScript Next: New features of ECMAScript 2015, 2016, and beyond. The section links point to the parts of the book it draws on.