Javascript Canvas Game Monster Gates
Monsters should spawn randomly at gate opening in arena. First, add the gates array.
var gates = new Array(
new Array(canvas.width / 2, 0),
new Array(canvas.width, canvas.height / 2),
new Array(canvas.width / 2, canvas.height),
new Array(0, canvas.height / 2)
);
Then we want to spawn a monster randomly.
var spawnMonster = function() {
gate = Math.floor(Math.random() * 4) + 1;
monster.x = gates[gate - 1][0];
monster.y = gates[gate - 1][1];
};
And change this in the slayMonster function
monster.x = 100;
monster.y = 100;
for
spawnMonster();
Added bonus, we can have the monster start at a spawn point by adding.
spawnMonster();
And change the monster array values.
monster.x = -100;
monster.y = -100;
This means that the monster will be drawn off the screen before it is spawned.
You can download this example.