Simplifying the code

Once you get a working code, don't stop. Try to make the code more readable and maintainable, possibly reducing the number of lines.

Here's my simplified version of placeSnake function:

private function placeSnake():void { addChild(snakeContainer); var col:uint=Math.floor(Math.random()*(FIELD_WIDTH-10))+5; var row:uint=Math.floor(Math.random()*(FIELD_HEIGHT-10))+5; var tmpCol,tmpRow,evenDir:uint; snakeDirection=Math.floor(Math.random()*4); the_snake=new the_snake_mc(col*TILE_SIZE, row*TILE_SIZE,snakeDirection+1); snakeContainer.addChild(the_snake); // remove the entire switch for (var i:uint=1; i<=2; i++) { evenDir = snakeDirection%2; tmpCol = col+i*(1-evenDir)*(1-snakeDirection); tmpRow = row+i*(2-snakeDirection)*evenDir; ...

Get Flash Game Development by Example now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.