What is the complete error message you are geting? (copy/paste to post)


import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Playground here. * * @author (your name) * @version (a version number or a date) */ public class Playground extends World { Snake[] snakeBody; String segment; int direction; private int speed; private int speedX; private int speedY; boolean eaten; /** * Constructor for objects of class Playground. * */ public Playground() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(600, 400, 1); direction = 0; eaten = false; speed = 2; speedX = speed; speedY = 0; snakeBody = new Snake[3]; for(int i=0; i<snakeBody.length; i++) { snakeBody[i] = new Snake(); addObject(snakeBody[i], 90+(snakeBody.length-i)*15, 30); // adding snake actor to the world } } public void checkKey() { if(Greenfoot.isKeyDown("right")) // checks if right key is pressed { if(direction==1 || direction==3) { direction = 0; speedX = speed; speedY = 0; } } else if(Greenfoot.isKeyDown("down")) // checks if down key is pressed { if(direction==0 || direction==2) {direction = 1; speedX = 0; speedY = speed; } } else if(Greenfoot.isKeyDown("left")) // checks if left key is pressed { if(direction==1 || direction==3) {direction = 2; speedX = -speed; speedY = 0; } } else if(Greenfoot.isKeyDown("up")) // checks if up key is pressed { if(direction==0 || direction==2) { direction = 3; speedX = 0; speedY = -speed; } } } public void moveIt() { int previousLocationX = snakeBody[0].getX(); int previousLocationY = snakeBody[0].getY(); snakeBody[0].setLocation(snakeBody[0].getX()+speedX, snakeBody[0].getY()+speedY); if(snakeBody[0].getX()!=previousLocationX || snakeBody[0].getY()!=previousLocationY) { for(int i=0; i<snakeBody.length; i++) { previousLocationX = snakeBody[i].getX(); previousLocationY = snakeBody[i].getY(); snakeBody[i].setLocation(snakeBody[i].getX()+speedX, snakeBody[i].getY()+speedY); } } } /** * This method will update the length of the Snake. */ private void growSnake(int x, int y, int rotation) { Snake s = new Snake(); Snake oldSnake[] = new Snake[snakeBody.length]; for(int i=0; i<snakeBody.length; i++) { oldSnake[i] = snakeBody[i]; } snakeBody = new Snake[snakeBody.length+1]; for(int i=0; i<snakeBody.length-1; i++) { snakeBody[i] = oldSnake[i]; } snakeBody[snakeBody.length-1] = s; addObject(snakeBody[snakeBody.length-1], x, y); // adds new snake body actor } public void act() { checkKey(); moveIt(); } }
public void moveIt() { int previousLocationX = snakeBody[0].getX(); int previousLocationY = snakeBody[0].getY(); snakeBody[0].setLocation(snakeBody[0].getX()+speedX, snakeBody[0].getY()+speedY); if(snakeBody[0].getX()!=previousLocationX || snakeBody[0].getY()!=previousLocationY) { for(int i=1; i<snakeBody.length; i++) { previousLocationX = snakeBody[i].getX(); previousLocationY = snakeBody[i].getY(); snakeBody[i].setLocation(snakeBody[i].getX(), snakeBody[i].getY()); } } }
for (int i = 1; i < snakeBody.length(); i++) { int holdX = snakeBody[i].getX(); int holdY = snakeBody[i].getY(); snakeBody[i].setLocation(previousLocationX, previousLocationY); previousLocationX = holdX; previousLocationY = holdY; }