I have all i need for a Snake game but I'm having difficulty creating the array so that the tail follows the head.
This part in SnakeHead adds a SnakeBody
This code adds a SnakeBod next to SnakeHead but because I don't have an array so the body bits pile up behind the head. HELP :(
private void addSnakeBody()
{
int currX = getX(); //get current x co-ordinate
int currY = getY(); // get current y co-ordinate
getWorld().addObject(new SnakeBody(), currX-offsetX, currY-offsetY); //add SnakeBody at location of SnakeHead
} private void growSnake()
{
int offsetX=0;
int offsetY=0;
int currX = getX(); //get current x co-ordinate
int currY = getY(); // get current y co-ordinate
List getSnakeHead=getWorld().getObjects(SnakeHead.class); //get the SnakeHead actor from the world.
if(!getSnakeHead.isEmpty ()) //if there is no SnakeHead
{
SnakeHead thisSnakeHead=(SnakeHead)getSnakeHead.get(0); //is there a SnakeHead in the current location?
offsetX=thisSnakeHead.offsetX; //the x co-ordinate of SnakeHead
offsetY=thisSnakeHead.offsetY; //the y co-ordinate of SnakeHead
}
setLocation(currX+offsetX, currY+offsetY); //
}
