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.

image22

Create a new project at woofjs.com and use the following code:

// Set the backdrop image
setBackdropURL("http://www.codemahal.com/sprites/grass_background.png")

// Create the enemy
var enemy = new Image({
  url: "http://www.codemahal.com/sprites/enemy.png",
  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
  enemy.pointTowards(enemyGoTo)
})

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