Game Development Reference
In-Depth Information
iterate through that array, and tell every window where the snake is. The code for
this is as follows:
// Define a few global variables in order to
keep the code shorter and simpler
var isPaused = true;
var timer;
var dirChange = 100;
var btn = document.createElement("button");
btn.textContent = "Add Window";
btn.addEventListener("click", function(event){
var left = frames.length * frames.width +
frames.margin * frames.length;
frames[frames.length] = window.open("/packt/
snake-v2/snake-panels.html", "",
"width=" + frames.width + "," +
"height=" + frames.height + "," +
"top=100, left=" + left);
isPaused = false;
clearTimeout(timer);
play();
}, false);
document.body.appendChild(btn);
// We'll close all the windows we have opened
to save us the
// trouble of clicking each window when we want
them closed
function closeAll() {
for (var i = 0, len = frames.length; i < len;
i++) {
frames[i].close();
}
}
window.onunload = closeAll;
Now, the real magic happens in the following method. All that we'll do is update the
snake's position, then tell each window where the snake is. This will be done by
converting the snake's position from world coordinates to canvas coordinates (since
Search WWH ::




Custom Search