Jump to content

Issue with the first Phaser.js tutorial


pollofritto
 Share

Recommended Posts

Hi guys,

i'm completely new to Phaser (to be honest i'm completely new to javascript as well :P ) i started a tutorial found on the web which is going to teach me how to build an "old nokia style" snake game.

 

i had no problems at all following the beginning of the tutorial but at some point i got an error calculating the position/movement of the snake (the snake is an array). i really can't figure out what is happening. I also tried to copy/paste the tutorial .js files (from the chrome dev console actually in order to avoid some syntax mistake on tutorial itself... the tutorial provides a preview of the game for every steps) but the error is still there while the tutorial preview is doing amazing!

 

the error i got is this one: Uncaught TypeError: Cannot read property 'x' of undefined

 

this is the code:

firstCell = snake[snake.length - 1],lastCell = snake.shift(),oldLastCellx = lastCell.x,oldLastCelly = lastCell.y;

the issue is snake.shift() . It becomes undefined at some point.

i also think that the debug breaks something because if i try to debug the tutorial preview i got the same error i got on my code!!!!

 

this is the folder where you can find the folder of my project

 

https://www.dropbox.com/home/Phaser/phaser_test

 

 

i hope you can help me!

thanks. :wub:

 

 

 

 

Link to comment
Share on other sites

I don't have time to go through your code, but I suspect the array becomes empty at some point, causing the error. You can guard against this by checking the array length before you shift it. But I suspect that will just hide a deeper bug, as a snake 'body array' should never become empty really (as the snake only ever gets longer).

Link to comment
Share on other sites

Uaaaa thank you rich! you mede me think when you say: the snake should never become empty... i made a couple of changes to my code.

The error is gone! the snake still does not move like it should be but it's better than before.. at least i am learning something  :D

 

well.. it was my code obviously..

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...