Workshop › Asteroids › Step 22 of 41
A hit rock breaks: it is removed, and unless it was the smallest, two rocks one size smaller are born at the same spot, each flying its own random way. Big → two medium → two small → gone.
```js
function breakAsteroid(asteroid) {
asteroids = asteroids.filter((a) => a !== asteroid)
if (asteroid.size > 1) {
asteroids.push(makeAsteroid(asteroid.x, asteroid.y, asteroid.size - 1))
asteroids.push(makeAsteroid(asteroid.x, asteroid.y, asteroid.size - 1))
}
}
```filter((a) => a !== asteroid) keeps every rock except this one, in a new list.size - 1 are added at its position.Above the keydown listener write breakAsteroid, followed by an empty line.
Skills: Arrays and objects, Game states
← When do two circles touch? Bullets hit rocks →