Workshop › Mario-style Platformer › Step 27 of 37
The second rule of a walker: after moving, look at the ground just ahead of the front foot. Nothing to stand on there? Turn around. This one probe makes enemies patrol platforms instead of marching into every pit.
```js
// Turn around at ledges: is there ground just ahead of the front foot?
if (enemy.grounded) {
const aheadX = enemy.dir > 0 ? enemy.x + enemy.w : enemy.x - 1
if (!solidAt(aheadX, enemy.y + enemy.h)) enemy.dir = -enemy.dir
}
```aheadX is one step past the front edge: the right edge when walking right, one pixel left of the left edge otherwise.enemy.y + enemy.h is the first pixel below the feet, where the floor should be.In updateEnemy, under moveY(enemy), write the comment and the if block.
Skills: Collision detection
← Enemies that walk Lives and a fresh start →