liveBook · Manning

getSpriteProperty

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

A game-framework helper method for retrieving a sprite's property value regardless of whether it is stored statically, as an instance property, or as a method.

When a new game object needs to start where a sprite is, use getSpriteProperty(sprite, name) to read the value. It handles values stored as static properties, instance properties, or methods. 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%@!

A ship reads its position and rotation to create a rocket with matching values. section 29.3

The same pattern supplies a comet's position, size, and speed when creating smaller shards, and an object's position when placing an explosion. section 29.4 section 29.5

multiply() {
  const x = getSpriteProperty(this, 'x');
  const y = getSpriteProperty(this, 'y');
  let size = getSpriteProperty(this, 'size');
  let speed = getSpriteProperty(this, 'speed');

  if (size < 5) {
    this.remove();                              1
    return;
  }
  size /= 2;                                    2
  speed *= 1.5;                                 2
  const removeOnExit = size < 5;                3
  const r = Math.random() * Math.PI;
  const ninetyDeg = Math.PI/2;
  range(4).forEach(i => new Comet({             4
    x, y, size, speed, removeOnExit,
    rotation: r + ninetyDeg * i,
  }))
  this.remove();                                5
}
!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":7,\"ch\":48},{\"line\":7,\"ch\":49}],[{\"line\":11,\"ch\":11},{\"line\":11,\"ch\":12}],[{\"line\":10,\"ch\":10},{\"line\":10,\"ch\":11}],[{\"line\":11,\"ch\":48},{\"line\":11,\"ch\":49}],[{\"line\":14,\"ch\":28},{\"line\":14,\"ch\":29}],[{\"line\":10,\"ch\":10},{\"line\":10,\"ch\":11}],[{\"line\":11,\"ch\":48},{\"line\":11,\"ch\":49}],[{\"line\":14,\"ch\":28},{\"line\":14,\"ch\":29}],[{\"line\":12,\"ch\":48},{\"line\":12,\"ch\":49}],[{\"line\":15,\"ch\":8},{\"line\":15,\"ch\":9}],[{\"line\":6,\"ch\":13},{\"line\":6,\"ch\":14}],[{\"line\":11,\"ch\":13},{\"line\":11,\"ch\":14}],[{\"line\":12,\"ch\":30},{\"line\":12,\"ch\":31}],[{\"line\":19,\"ch\":48},{\"line\":19,\"ch\":49}]]"}
!@%STYLE%@!

Comet splitting reads several properties before using them to create four smaller comets. section 29.4

new Explosion({
  x: getSpriteProperty(this, 'x'),
  y: getSpriteProperty(this, 'y'),
});

An explosion receives the sprite's position through helper calls. section 29.5

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

How deep it goes
Defines the helper's purpose and demonstrates its use, but does not explain its implementation.
Examples
Uses x, y, rotation, size, and speed reads to configure rockets, comet shards, and explosions.
What you can do after
Apply the game-framework helper to read sprite values when configuring new game objects.
How it connects
Connects sprite properties with JavaScript classes, game objects, collision handling, and object creation.

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.