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].parent.name) { case "fruit" : removeChild(fruit); placeStuff(); break; } }
Test the movie and eat a fruit: it will disappear and a new fruit will appear in another location.
In the picture, the snake eats a fruit...