diff --git a/js/main.js b/js/main.js index 63308a5..4447898 100644 --- a/js/main.js +++ b/js/main.js @@ -1,4 +1,4 @@ -(function () { +(function (spriteObject, enemyObject) { 'use strict'; var canvas = document.querySelector("canvas"); var drawingSurface = canvas.getContext("2d"); @@ -35,28 +35,23 @@ background.height = 640; sprites.push(background); - var hero = Object.create(spriteObject); hero.image = heroImage; hero.x = canvas.width / 2 - hero.width / 2; hero.y = canvas.height - hero.height; sprites.push(hero); - var assetsLoaded = 0; - var enemyFrequency = 120; var enemyTimer = 0; - var LOADING = 0; var PLAYING = 1; var OVER = 2; var gameState = LOADING; //Key codes and directions - var RIGHT = 39; var LEFT = 37; var SPACE = 32; @@ -66,7 +61,6 @@ var shoot = false; var spaceKeyIdDown = false; - window.addEventListener("keydown", function (event) { switch (event.keyCode) { case LEFT: @@ -81,9 +75,9 @@ spaceKeyIdDown = true; } } + event.preventDefault() }, false); - window.addEventListener("keyup", function (event) { switch (event.keyCode) { case LEFT: @@ -95,15 +89,16 @@ case SPACE: spaceKeyIdDown = false; } + event.preventDefault() }, false); -//Start here + //Start here update(); - + function update() { - requestAnimationFrame(update, canvas); - + requestAnimationFrame(update, canvas); + switch (gameState) { case LOADING: console.log("Loading..."); @@ -115,19 +110,13 @@ endGame(); break; } - render(); - } - + render(); + } function loadHandler() { assetsLoaded++; if (assetsLoaded === assetsToLoad.length) { - - // for (var i = 0; i < images.length; i++) { - // images[i].removeEventListener("load", loadHandler, false); - // } - gameState = PLAYING; } } @@ -208,10 +197,7 @@ } - - function endGame() { - - } + function endGame() {} function render() { drawingSurface.clearRect(0, 0, canvas.width, canvas.height); @@ -226,7 +212,6 @@ sprite.width, sprite.height ); } - } } @@ -277,6 +262,7 @@ enemy.startAnimation(); sprites.push(enemy); enemies.push(enemy); + console.log(enemy) } function destroyEnemy(enemy) { @@ -289,6 +275,4 @@ removeObject(enemy, sprites); } } - - -}()); +}(spriteObject, enemyObject)); diff --git a/js/objects.js b/js/objects.js index 73a323c..f0da1b2 100644 --- a/js/objects.js +++ b/js/objects.js @@ -1,63 +1,60 @@ -var spriteObject = - { - image: "", - sourceX: 0, - sourceY: 0, - sourceWidth: 80, - sourceHeight: 110, - width: 80, - height: 110, - x: 0, - y: 0, - vx: 0, - vy: 0, - startPointX: 0, - startPointY: 100, - endPointX: 0, - endPointY: 0, - visible: true, +var spriteObject = { + image: "", + sourceX: 0, + sourceY: 0, + sourceWidth: 80, + sourceHeight: 110, + width: 80, + height: 110, + x: 0, + y: 0, + vx: 0, + vy: 0, + startPointX: 0, + startPointY: 100, + endPointX: 0, + endPointY: 0, + visible: true, - COLUMNS: 9, - numberOfFrames: 1, - currentFrame: 0, - firstAnimationFrame: 0, - lastAnimationFrame: 0, - - - //Getters - centerX: function () { - return this.x + (this.width / 2); - }, - centerY: function () { - return this.y + (this.height / 2); - }, - halfWidth: function () { - return this.width / 2; - }, - halfHeight: function () { - return this.height / 2; - }, - vectorA: function () { - return (this.endPointY - this.startPointY) / (this.startPointX - this.endPointX) * -1; - }, - vectorB: function () { - return this.endPointY - (this.vectorA() * this.endPointX); - }, - updateAnimation: function () { - this.sourceX = Math.floor(this.currentFrame % this.COLUMNS) * this.sourceWidth; - this.sourceY = Math.floor(this.currentFrame / this.COLUMNS) * this.sourceHeight; - }, - startAnimation: function () { - this.currentFrame++; - if (this.currentFrame > this.lastAnimationFrame) { - this.currentFrame = this.firstAnimationFrame; - } - this.updateAnimation; - setTimeout(this.startAnimation, 100); + COLUMNS: 9, + numberOfFrames: 1, + currentFrame: 0, + firstAnimationFrame: 0, + lastAnimationFrame: 0, + //Getters + centerX: function () { + return this.x + (this.width / 2); + }, + centerY: function () { + return this.y + (this.height / 2); + }, + halfWidth: function () { + return this.width / 2; + }, + halfHeight: function () { + return this.height / 2; + }, + vectorA: function () { + return (this.endPointY - this.startPointY) / (this.startPointX - this.endPointX) * -1; + }, + vectorB: function () { + return this.endPointY - (this.vectorA() * this.endPointX); + }, + updateAnimation: function () { + this.sourceX = Math.floor(this.currentFrame % this.COLUMNS) * this.sourceWidth; + this.sourceY = Math.floor(this.currentFrame / this.COLUMNS) * this.sourceHeight; + }, + startAnimation: function () { + this.currentFrame++; + if (this.currentFrame > this.lastAnimationFrame) { + this.currentFrame = this.firstAnimationFrame; } + this.updateAnimation; + setTimeout(this.startAnimation, 100); - }; + } +}; var enemyObject = Object.create(spriteObject);