Snake: Will not ad cells to array with push. Why not?

#1

I am translating from Processing:
http://vestera.as/processing/?id=23

to p5.js:
https://editor.p5js.org/steinlav/sketches/1rkgu_AGI

Problem in p5.js line 57
Why is it not possible to ad object “head” to the array “body”?
I have stared at this for hours :frowning: :slight_smile:

function update() {
  // Puts the head in the array. Why does this not work?
  **body.push(head);**
  if (grow > 0) { 
    // Shall grow, Head is moving. Tail stays behind.
    grow--;  
  } else { 
    // Removing tail cell
    body.splice(0, 1); 
  }
1 Like

#2

It just looks like a small typo. I started by console.log(body) to see if the push was working. The body was growing so. It’s not displaying right. The for loop has a typo in it.

// on line 46
for (i = 0; i < body.lenght; i++) {
// should be change to
for (let i = 0; i < body.length; i++) {

This is one of the disadvantages of javascript where it won’t always tell you if a object key doesn’t exist.

2 Likes

#3
1 Like

#4

:crazy_face: Thank you!! :slightly_smiling_face:

0 Likes