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 :(
1 2 3 4 5 6 7 8 9 | 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 } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | 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); // } |