I have a function that adds an item to a random location to a grid (this is done on a timer event) and in this function i have a check that sees if the item hits the player. and if it does it calls the function. however it gives me the error when i call that function again. it might be because the function is a (event:TimerEvent)?
below is the function
private function placeFood(event:TimerEvent = null):void{
var rndI:uint = Math.floor(Math.random() * DIM); //sets a random integer based on the the floor
var rndJ:uint = Math.floor(Math.random() * DIM);
var rndX:Number = grid[rndI][rndJ].x; // sets a grid position for the food item to go
var rndY:Number = grid[rndI][rndJ].y;
if(_foodMap[int(rndX)] == null){
_foodMap[rndX] = [];
}
if(_foodMap[int(rndX)][int(rndY)] == null){
_foodMap[rndX][rndY] = food;
}
food = makeItem(Math.random() * 0xFFFFFF);// random color
food.x = rndX;
food.y = rndY;
addChild(food); //adds the food to the board
for (var i:uint = 0; i < snake.length; i++){
if (rndY == snake[i].y && rndX == snake[i].x){
placeFood();
}
}
}
If you call the function manually without the TimerEvent which the function is expecting you will get the error. So you need to define that the param’s default value is null:
For the food map create the multidimensional array which will contains the references to foods.
Then in the place when you add the food then also add it to the food map like i did below so you could check if the location where the snake’s head is is empty.
Then if both
_foodMap[int(snakesHeadX)] == nulland_foodMap[int(snakesHeadX)][int(snakesHeadY)] == nullthen it means the location of snake’s head is empty.