vmars316 Posted April 21, 2016 Share Posted April 21, 2016 Hello & Thanks , I am getting 'Uncaught SyntaxError: Unexpected identifier' on : Cowpie.prototype.update = function() { for (var i = cowpies.length - 1; i >= 0; i--) { cowpieCount = i ; if (cowpies[i].cowpie.idActive) { self.y = self.y+ (self.speedY * self.directionY); // always -1 } } else { cowpies[i].pop(this.cowpie);} } } and if I comment the above out , then I get same for : var cowpie = new Cowpie(); and so on , down the line . Until I dismiss the whole section below . var Cowpie = (function () { var self = this; this.idTag = 'cowpie'; this.idActive = true; this.x = thrower.x; this.y = thrower.y; this.width= 64; this.height = 64; this.speedX = 0; this.speedY = 5; this.visible = true; this.directionY = -1; this.moveMe = false ; } // Cowpie.prototype.update = function() { for (var i = cowpies.length - 1; i >= 0; i--) { cowpieCount = i ; if (cowpies[i].cowpie.idActive) { self.y = self.y+ (self.speedY * self.directionY); // always -1 } } else { cowpies[i].pop(this.cowpie);} } } // var cowpie = new Cowpie(); // var Thrower = function () { var self = this; this.idTag = 'thrower'; this.x = (canvas.width / 2); this.y = canvas.height - 64; this.width= 64; this.height = 64; this.speedX = 1; this.speedY = 0; this.visible = true; this.directionX = 5; this.moveMe = false ; } Thrower.prototype.update = function() { self.x = self.x + (self.speedX * self.directionX); // always either +1 or -1 } } // var thrower = new Thrower(); thrower.idTag ; // = 'thrower'; // function startGame() { myGameArea.start(); } 1 Link to comment Share on other sites More sharing options...
Ingolme Posted April 22, 2016 Share Posted April 22, 2016 It seems you have an unclosed parenthesis on this line: var Cowpie = (function () { It would be helpful if you told us what line of code the browser indicated the error was on. Another issue is on these lines: this.x = thrower.x; this.y = thrower.y; thrower has not been declared yet. In the specific block of code you pointed out, self is not accessible. You can use this and it will work there because the method belongs to the prototype. Link to comment Share on other sites More sharing options...
vmars316 Posted April 22, 2016 Author Share Posted April 22, 2016 (edited) Thanks , this line was problem : Cowpie.prototype.update = function() { then this one : var cowpie = new Cowpie(); I'll try to do better next time . A related question , I don't have much experience with arrays : Array defined like this : var cowpies = [document.getElementById("cowpie")]; I am getting a '174 Uncaught TypeError: Cannot read property 'idActive' of undefined' on this line : if (!cowpies.cowpie.idActive) {cowpies.pop(cowpie);}; in : if(e.keyCode == 87 || event.keyCode == 38) { // shoot W or upArrow // check cowpies array if idActive = false Delete item // 1st active cowpie already set up in function startGame() for (var i = cowpies.length - 1; i >= 0; i--) { cowpiesCount = i ; if (!cowpies[i].cowpie.idActive) {cowpies[i].pop(cowpie);}; alert("cowpies[i].cowpie.idActive = " + i + " "+ cowpies[i].cowpie.idActive ); } cowpie = new Cowpie; cowpies[i].cowpie.update; cowpies.push(cowpie); } I think I am messing up on the array syntax . Pls , what should it be ? Thanks Edited April 22, 2016 by vmars316 Link to comment Share on other sites More sharing options...
Ingolme Posted April 22, 2016 Share Posted April 22, 2016 There are at least two issues. 1. cowpie is not a property of an HTML element: cowpies[i].cowpie.idActive If the element has a property "idActive" you would access it like this cowpies[i].idActive 2. pop() is a method of cowpies, not of cowpies. And the variable cowpie is undefined: cowpies[i].pop(cowpie) pop() always removes the last element of an array and it does not take any parameters. You probably want to use the splice() method instead. Link to comment Share on other sites More sharing options...
vmars316 Posted April 22, 2016 Author Share Posted April 22, 2016 You probably want to use thesplice() method instead. Yes , Thanks , I am reading the Arrays Tutorial http://www.w3schools.com/js/js_arrays.asp now. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now