Eating fruits

Once the fruit is placed, eating is easy, let's say a piece of cake. A fruit cake, of course.

The idea: In the same way we checked for an empty tile to place the fruit in, we'll check for the tile occupied by the snake's head looking for a fruit. If we find a fruit, we have to remove it and place a new one elsewhere.

The development: Once the head has moved, we have to retrieve its middle point and see if there's a fruit under such point. Before the end of onEnterFr function, add this code:

var point_to_watch:Point=new Point(the_head.x+TILE_SIZE/2,the_head.y+TILE_SIZE/2); var children_in_that_point:Array=stage.getObjectsUnderPoint(point_to_watch); for (var i:uint=0; i<children_in_that_point.length; i++) { switch (children_in_that_point[i] ...

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.