Workshop › Mario-style Platformer › Step 26 of 37
Here the work on moveX and moveY pays off: they take any body. An enemy walks one pixel a frame in its direction,
turns around when moveX reports a wall, and falls and lands with the same gravity as the player.
```js
function updateEnemy(enemy) {
enemy.vx = enemy.dir
if (moveX(enemy)) enemy.dir = -enemy.dir
enemy.vy = Math.min(MAX_FALL, enemy.vy + GRAVITY)
moveY(enemy)
}
for (const enemy of enemies) updateEnemy(enemy)
```vx = dir: one pixel per frame left or right.moveX returns true at a wall; then dir = -dir turns the enemy around.moveY exactly as for the player.update calls it for every enemy, after the player.function update() { write updateEnemy, with an empty line after it.update, under updatePlayer(), write the enemy loop.Skills: Velocity and gravity, Functions
← Enemies Do not walk off ledges →