Snake Game, Snake won't Grow

I’m trying to practice a bit of my actionScript by programming a small snake game. I’ve been working on it for about an hour now, but for the last 15 minutes or so, I’ve been trying to solve this problem. I’m pretty sure that I know where the problem lies, but I’m not sure why it works the way it does.

The problem is that my snake won’t grow, actually, it won’t get longer than 3 cells even if I set it at the beginning. I think the problem is with the way I am passing the previous values of the positions of the cells through the array, but I’m not sure.

Here’s my code:


//Starting Variables
cellSize = 10;
gridSize = 30;
speed = 1;
startingSnakeSize = 10;
//Initialization Function
function initialize() {
 //Set all game variables to their original values
 snakeSize = startingSnakeSize-1;
 snakeDirection = "Right";
 //Set up grid
 k = 0;
 createEmptyMovieClip("grid", -1);
 for(i=0; i<gridSize; i++) {
  for(j=0; j<gridSize; j++) {
  //Make a row of the grid
  grid.attachMovie("cell", "cellR"+i+"C"+j, k);
  grid["cellR"+i+"C"+j]._x = j*cellSize;
  grid["cellR"+i+"C"+j]._y = i*cellSize;
  grid["cellR"+i+"C"+j]._width = cellSize;
  grid["cellR"+i+"C"+j]._height = cellSize;
  k++;
  }
 }
 //Set up Snake
 createEmptyMovieClip("snake", 0);
 snakePos = new Array();
 //Set up Arrays
 for(i=0; i<snakeSize; i++) {
  snakePos* = new Array();
  snakePos*[0] = i;
  snakePos*[1] = 0;
 }
 currentPos = new Array();
 currentPos[0] = snakePos[snakeSize-1][0]+1;
 currentPos[1] = snakePos[snakeSize-1][1];
 //Draw Snake
 drawSnake(snakeSize);
}
function drawSnake(snakeLength) {
 for(i=0; i<snakeLength; i++) {
  j = i-1;
  snake.attachMovie("snake", "snake"+i, i);
  snake["snake"+i]._x = snakePos*[0]*cellSize;
  snake["snake"+i]._y = snakePos*[1]*cellSize;
  if(i > 0) {
   snakePos*[0] = snakePos[j][0];
   snakePos*[1] = snakePos[j][1];
  }
  //trace(snakePos*[0]+", "+snakePos*[1]);
 }
 snake.attachMovie("snake", "startingSnake", snakeLength);
 snake.startingSnake._x = currentPos[0]*cellSize;
 snake.startingSnake._y = currentPos[1]*cellSize;
 snakePos[0][0] = currentPos[0];
 snakePos[0][1] = currentPos[1];
}
onLoad = initialize();
onEnterFrame = function() {
 //Move Snake's Position
 if(snakeDirection == "Right") {
  currentPos[0]++;
 } else if(snakeDirection == "Down") {
  currentPos[1]++;
 } else if(snakeDirection == "Left") {
  currentPos[0]--;
 } else if(snakeDirection == "Up") {
  currentPos[1]--;
 }
 //Draw Snake
 drawSnake(snakeSize);
 //Check for hit with Apple
 //If hit, move apple; add to score; make snake longer
 //Check for hit with wall
 //If hit, gameOver
}
//Listen for Arrow Keys and Change Direction Variable
keyListener = new Object();
keyListener.onKeyDown = function() {
 if(Key.isDown(Key.RIGHT)) {
  if(snakeDirection != "Left") {
   snakeDirection = "Right";
  }
 } else if(Key.isDown(Key.DOWN)) {
  if(snakeDirection != "Up") {
   snakeDirection = "Down";
  }
 } else if(Key.isDown(Key.LEFT)) {
  if(snakeDirection != "Right") {
   snakeDirection = "Left";
  }
 } else if(Key.isDown(Key.UP)) {
  if(snakeDirection != "Down") {
   snakeDirection = "Up";
  }
 }
}
Key.addListener(keyListener);

The problem lies in the drawSnake() function I believe:


function drawSnake(snakeLength) {
 for(i=0; i<snakeLength; i++) {
  j = i-1;
  snake.attachMovie("snake", "snake"+i, i);
  snake["snake"+i]._x = snakePos*[0]*cellSize;
  snake["snake"+i]._y = snakePos*[1]*cellSize;
  if(i > 0) {
   snakePos*[0] = snakePos[j][0];
   snakePos*[1] = snakePos[j][1];
  }
  //trace(snakePos*[0]+", "+snakePos*[1]);
 }
 snake.attachMovie("snake", "startingSnake", snakeLength);
 snake.startingSnake._x = currentPos[0]*cellSize;
 snake.startingSnake._y = currentPos[1]*cellSize;
 snakePos[0][0] = currentPos[0];
 snakePos[0][1] = currentPos[1];
}

Thanks a ton for you help :thumb: :slight_smile: