Make an enemy move around randomly in WoofJS

This tutorial explains how to make an enemy patrol or move around randomly on its own in WoofJS, as shown below.


Create a new project at and use the following code:

// Set the backdrop image

// Create the enemy
var enemy = new Image({
  url: "",
  width: 30, 
  height: 30,
  x: 50,
  y: 100

// Use an invisible or tiny image as a marker
// This marker is where the enemy will point towards and go to.
var enemyGoTo = new Image({
  width: 1, 
  height: 1,

// Every two seconds...
every(2, 'second', () => {
  // Move the invisible marker to a random position
  enemyGoTo.x = randomX()
  enemyGoTo.y = randomY()
  // Make the enemy point towards the invisible marker

forever(() => {
  // Make the enemy move three spaces every frame