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
