Can anyone assist? I have written code for a game to help memory of Architecture in CPUs (GCSE) which involves a character moving around different text objects. When they hit the correct text object it changes colour from Orange to green and if this happens I want the counter to increase by 1. (if they hit the wrong text object then the colour changes to red (currently). The issue I am getting is a null pointer error which I believe relates to the non initialisation of the counter to zero but I do not know where. Can anyone see what I need to do to sort the error. one option would be a try catch but the score would not increase.
Here is my world code
public class Level1 extends greenfoot.World
{
Good component;// reference the Good component
Counter counter; // reference a Score Object
/**
* Constructor for objects of class MyWorld.
*
*/
public Level1()
{
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
super(600, 400, 1);
prepare();
}
/**
* Prepare the world for the start of the program.
* That is: create the initial objects and add them to the world.
*/
private void prepare()
{
Robot robot = new Robot();
addObject(robot,150,200);
Platform platform = new Platform();
addObject(platform,255,250);
Platform platform2 = new Platform();
addObject(platform2,390,355);
Platform platform3 = new Platform();
addObject(platform3,130,250);
Platform platform4 = new Platform();
addObject(platform4,512,356);
Good component = new Good("CPU");
addObject(component,543,311);
Platform platform5 = new Platform();
addObject(platform5,210,115);
Platform platform6 = new Platform();
addObject(platform6,482,111);
Platform platform7 = new Platform();
addObject(platform7,361,115);
Bad component2 = new Bad("CU");
addObject(component2,514,64);
Counter counter = new Counter("Score: ");
addObject(counter,294,20);
}
public Good getGood(){
return component;
}
public Counter getCounter(){
return counter;
}
}
My Good (correct text object inherits from basic Component text object which inializes the starting colour of Orange)Code
public class Good extends Component
{
private String componentName;
// private int level = 1;//world level
//boolean stillOnL1 = false; // checking level1
//boolean stillOnL2 = false; // checking level1
boolean catch1 =false;
boolean catch2 =false;
boolean catch3 =false;
int caught = 0;
//World nl; //setting a new world
// Score score; //reference a score object
public Good(String aComponentName)
{
super(aComponentName);//invokes the constructor of Component the parent class
componentName = aComponentName;
Score score = new Score();
}
/**
* Act - do whatever the Good wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
start();
}
/**
* Changes color of the text box to GREEN
*/
public void changeColourGreen()
{
setImage(new GreenfootImage(componentName, 20,
Color.BLACK, Color.GREEN));
}
/**
* Checks if the robot is touching the correct component class
* If true changes the colour of the component
*/
public void start(){
//String text = "empty";
if (isTouchingText()){
addScore();
changeColourGreen();
//Greenfoot.delay(15);
//text = getText();
}
//return text;
}
public String getText(){
return componentName;
}
public void addScore(){
World myWorld = getWorld();
//if (myWorld instanceof Level1){
Level1 l1 = (Level1) myWorld;
Counter s = l1.getCounter();
s.setValue( 1);//s.add(1);
//}
}
}
Finally my main character class
public class Robot extends Actor
{
//instance variables
private int speed = 4; //horizontal speed
private int vSpeed = 0; //vertical speed
private int acceleration = 1;// speed of fall
private int jumpHeight = 10;
/**
* Act - do whatever the Robot wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
checkKeys();
checkFall();
}
/**
* Checks if the robot is touching a component class
* @returns true is contact is mad and false otherwise
*/
public boolean isTouchingText()
{
return isTouching(Component.class);
}
/**
* Checks to see if robot is on the platform using the on Platform method
* if true the value of vertical speed is reduced to 0
* if false the robot continues to fall
*/
public void checkFall()
{
if(onPlatform()) {
vSpeed = 0;
}
else {
fall();
}
}
/**
* Method to check which key is being pressed and move accordingly
* implementing further methods moveLeft(), moveRight() and jump()
* using A, D keys and the spacebar
* this avoids cluttering the Act() method
* TODO set image direction
* inpired by M Kolin
*/
public void checkKeys()
{
if(Greenfoot.isKeyDown("A"))
{
setImage("robot-L.png");
moveLeft();
}
if(Greenfoot.isKeyDown("D"))
{
setImage("RobotR1.png");
moveRight();
}
if(Greenfoot.isKeyDown("space"))
{
jump();
Greenfoot.delay(10);
fall();
}
//changeScore();
}
/**
* Jump by the value of jumpHeight, then invoke the fall method
*/
public void jump()
{
vSpeed = - jumpHeight;
fall();
}
/**
* Checks if the robot is on a platform, uses the offset method
* checks horizontal, vertical/quater of the robot image height
* against the Platform class
* @returns true
*/
public boolean onPlatform()
{
Actor standing = getOneObjectAtOffset(0, getImage().getHeight() / 4, Platform.class);
return standing !=null;
}
/**
* Fall by the value of vSpeed
*/
public void fall()
{
setLocation(getX(), getY()+ vSpeed);
vSpeed = vSpeed + acceleration;
}
/**
* Move to the left by the value of speed
*/
public void moveLeft()
{
setLocation(getX() - speed, getY());
}
/**
* Move to the left by the value of speed
*/
public void moveRight()
{
setLocation(getX() + speed, getY());
}
}