3 Replies - 238 Views - Last Post: 04 November 2017 - 01:37 AM Rate Topic: -----

#1 capitalcrunch  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 03-November 17

Question regarding uncaught typeerrors

Posted 03 November 2017 - 03:29 AM

I really don't know if this is where you're supposed to ask for help with this but if it doesn't belong here, I'll move it as soon as I know.

So I've been trying to recreate the old snake game with some conditions given to me by my teacher. The game so far was working until I made a couple of changes to the code like not hardcoding stuff into classes to make fixing errors easier but I ran into this bug. I have no idea how to fix it and I've been trying for 3 days now. This assignment is overdue by 3 days as well and my teacher already hates me so I'm turning to online help for this.

The error I'm getting says, "Snake.js:403 Uncaught TypeError: Cannot set property 'xtile' of undefined"

I wrote the program using Brackets and I know my coding is bad and beginner level but please help me fix this.
This is a part of the code that is apparently where the error is
function m_moveSnake(){
    var x;
    var y;
    if(g_allKeys[this.buttonUp] == true && g_allKeys[this.buttonLeft] != true && g_allKeys[this.buttonRight] != true){
        for(var u = 0; u < this.speed; u++){
            x = this.XpixelArray.pop();
            x = this.XpixelArray[0];
            this.XpixelArray.unshift(x);
            y = this.YpixelArray.pop();
            y = this.YpixelArray[0] - 1;
            
            if(y < 0)
                y = y + canvas.height;
            
            this.YpixelArray.unshift(y);
            this.x = this.XpixelArray[0];
            this.y = this.YpixelArray[0];
            
            if(speed_inc == true){
                this.speed += speed_inc;
                speed_inc = false;
            }
            
            if(speed_dec == true){
                this.speed -= speed_dec;
                speeddown_2 = false;
            }
                        
            for(var i = 0; i < this.length; i++){
                this.body[i].xtile = this.XpixelArray[i*snake_size];
                this.body[i].ytile = this.YpixelArray[i*snake_size];
            }    
        }
    }
}




The browser I'm using, Google Chrome, points to the for loop towards the end as where the error is btw

Is This A Good Question/Topic? 0
  • +

Replies To: Question regarding uncaught typeerrors

#2 xclite  Icon User is offline

  • I wrote you an code
  • member icon


Reputation: 1237
  • View blog
  • Posts: 4,029
  • Joined: 12-May 09

Re: Question regarding uncaught typeerrors

Posted 03 November 2017 - 05:25 AM

`this.body[i]` doesn't exist. You could print out or debug that line and see what `this.body` holds in it. I for one find it interesting that your loop goes from 0 to this.length but is iterating over this.body. Should it instead go from 0 to this.body.length?
Was This Post Helpful? 1
  • +
  • -

#3 capitalcrunch  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 03-November 17

Re: Question regarding uncaught typeerrors

Posted 03 November 2017 - 11:04 AM

Thank you for that. I made the appropriate changes to the code regarding the loop arguments and finally managed a senior to sit down with me and look over my code. Took us 4 hours but we managed to fix the numerous stupid errors that I failed to notice due to my stupidity.

I will be closing this topic now, if I can figure out how to that is.
Was This Post Helpful? 0
  • +
  • -

#4 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 1377
  • View blog
  • Posts: 4,312
  • Joined: 20-March 10

Re: Question regarding uncaught typeerrors

Posted 04 November 2017 - 01:37 AM

Don't be so hard on yourself you are not 'stupid' you are just inexperienced... you did the right thing getting a senior to help out...

And you can't close the Topic !

This is a teaching and learning website resource one day someone may have a similar problem and your OP may someday help someone else.

Cheers !
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1