This site requires JavaScript, please enable it in your browser!
Greenfoot back
KGCSTeacher
KGCSTeacher wrote ...

2018/8/28

Problems using Counter in Text Actor

KGCSTeacher KGCSTeacher

2018/8/28

#
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()); } }
davmac davmac

2018/8/28

#
A couple of things to make it easier to help:
  • use 'code' tags when posting code (read this!)
  • copy the stack trace, or at least give the line number where the exception actually happens!
The line number from the stack trace you get after the exception should correspond to a line in your code.
danpost danpost

2018/8/28

#
Also, copy/paste the error message you are getting (entire message). A lot of information can be gained from it. And post the entire class code (including import lines). This will allow one to quickly find the exact line where the error occurred.
davmac davmac

2018/8/28

#
For what it's worth though I think the problem is in your prepare method:
 Counter counter = new Counter("Score: ");
That creates a new variable, counter, which is local to the prepare method. What you most likely wanted to do was to initialise the counter field. In that case, just use assignment and not a declaration:
 counter = new Counter("Score: ");
I hope that helps.
danpost danpost

2018/8/28

#
The same can be said for the Good component field. That is, the line:
Good component = new Good("CPU");
would need to read
component = new Good("CPU");
for the field to obtain a value and for the getGood method to return that object (and not null).
You need to login to post a reply.