I've been learning CreateJS, and when I go to this part that has to add an eventlistener, it just won't work. The messages I put in addKey() and removeKey() just don't show up.
function startGame() {
window.addEventListener("keydown", addKey, false);
window.addEventListener("keyup", removeKey, false);
console.log("added listeners");
createjs.Ticker.setFPS(60);
createjs.Ticker.addEventListener("tick", tick);
}
function addKey(e) {
console.log("RUN!");
e = !e ? window.event : e;
var index = KEYS_ALLOWED.indexOf(e.keyCode);
if (true) {
keysPressed.push(e.keyCode);
}
}
function removeKey(e) {
console.log("RUN!");
e = !e ? window.event : e;
var index = keysPressed.indexOf(e.keyCode);
if (index > -1) {
keysPressed.splice(index, 1);
}
}
EDIT: Yes, the startGame is getting called. I put it in this, which is called when the body loads:
function init() {
canvas = document.getElementById("canvas");
stage = new createjs.Stage(canvas);
console.log("created stage");
createAssets();
placeAssets();
startGame();
}
By the way, it seems to work if I put
document.addEventListener
instead of
window.addEventListener
though the book I was using used the latter. Is there a reason for that, and will it affect me negatively if I just press on with it using the former?