Jump to content
vmars316

Need help with 'Uncaught SyntaxError: Unexpected identifier'

Recommended Posts

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();
}

  • Like 1

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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 by vmars316

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now

×