Workshop › Missile Command-style Defense › Step 7 of 35
An enemy missile starts at a random point on the top edge and aims at a random living city or at your base.
launch() makes one and adds it to the incoming list.
```js
let incoming // enemy missiles: { sx, sy, x, y, tx, ty, speed }
incoming = []
// A new enemy missile from a random point at the top towards a random living city (or the base).
function launch() {
const sx = Math.random() * canvas.width
const targets = cities.filter((c) => c.alive).map((c) => c.x).concat(BASE.x)
const tx = targets[Math.floor(Math.random() * targets.length)]
incoming.push({ sx, sy: 0, x: sx, y: 0, tx, ty: GROUND, speed: 0.8 })
}
```Math.random() * canvas.width is a random x from 0 to 480.targets: filter keeps the living cities, map turns each into its x, concat adds the base's x.Math.floor(Math.random() * targets.length) is a random index into that list.(sx, 0) and heads for (tx, GROUND) at 0.8 pixels per frame.let cities write let incoming with its comment.reset, under the cities line, write incoming = [].// Move a point ... comment, write launch with its comment, followed by an empty line.Skills: Arrays and objects
← Move towards a point One missile a second →