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

2018/4/3

Counter problems

HecticKiwi HecticKiwi

2018/4/3

#
Was trying to implement a counter for the asteroid tutorial but keep getting a java.lang.NullPointerException when running the scenario, as soon as the shot hits the asteroid it throws an error, below is the code for Space, Counter and Shot.
public class Space  extends World
{

    private Counter theCounter;

    /**
     * Constructor for objects of class Space.
     */
    public Space()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
        addObject(new Rocket(), 300, 200);
        Counter theCounter = new Counter();
        addObject(theCounter, 5, 5);
    }

    public Counter getCounter()
    {
        return theCounter;
    }
    
    /**
     * Prepare the world for the start of the program. That is: create the initial
     * objects and add them to the world.
     */
    private void prepare()
    {
    }
    
    public void act()
    {
        if (Greenfoot.getRandomNumber(1000) < 3) {
            addObject(new Asteroid(), 0, 20);
        }
    }
}
public class Counter  extends Actor
{
    private int totalCount = 0;

    public Counter()
    {
        setImage(new GreenfootImage("0", 20, Color.WHITE, Color.BLACK));
    }

    /**
     * Increase the total amount displayed on the counter, by a given amount.
     */
    public void bumpCount(int amount)
    {
        totalCount += amount;
        setImage(new GreenfootImage("" + totalCount, 20, Color.WHITE, Color.BLACK));
    }
}
public class Shot  extends Actor
{
    private Rocket myShip;

    /**
     * Constructor for a Shot. You must specify the ship the shot comes from.
     */
    public Shot(Rocket myShip)
    {
        this.myShip = myShip;
    }

    /**
     * Act - do whatever the Shot wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        int ypos = getY();
        if (ypos > 0) {
            ypos = ypos - 5;
            setLocation(getX(), ypos);
            Actor rock = getOneIntersectingObject(Asteroid.class);
            if (rock != null) {
                // We've hit an asteroid!
                hitAnAsteroid();
                getWorld().removeObject(rock);
                getWorld().removeObject(this);
            }
        }
        else {
            // I reached the top of the screen
            getWorld().removeObject(this);
        }
    }
    
    /**
     * This method gets called (from the act method, above) when the shot hits an
     * asteroid. It needs to do only one thing: increase the score counter.
     * (Everything else, such as removing the asteroid which was hit, is dealt
     * with in the act method).
     */
    private void hitAnAsteroid()
    {
       Space spaceWorld = (Space) getWorld();
       Counter counter = spaceWorld.getCounter();
       counter.bumpCount(5); 
       
       //What goes here????
       // We want to call the "bumpCount" method from the Counter class -
       // but how??!!
    }
}
Vercility Vercility

2018/4/3

#
Provide the error at least :( Also, im pretty sure y can't be <0 since you can't leave the world
danpost danpost

2018/4/3

#
Remove the declared type, Counter (the first word), from line 14 in your Space class. You do not want to create a new Counter variable there -- you just want to refer to the Counter field declared at line 4.
HecticKiwi HecticKiwi

2018/4/5

#
danpost wrote...
Remove the declared type, Counter (the first word), from line 14 in your Space class. You do not want to create a new Counter variable there -- you just want to refer to the Counter field declared at line 4.
Thanks so much, it worked for the tutorial but when implementing it into my project I have run into another error, "cannot find symbol - method getCounter()" on line 39 in the car class, not sure if it's because getCounter() is in MyWorld, any help would be appreciated.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class car extends Actor
{

    public void act() 
    {
        move(4);
        if(Greenfoot.isKeyDown("up"))
        {
            move(8);
        }
         if(Greenfoot.isKeyDown("down"))
        {
            move(-12);
        }
        if (Greenfoot.isKeyDown("left"))
        {
            turn(-5);
        }
        if (Greenfoot.isKeyDown("right"))
        {
            turn(5);
        }
        
        Actor fruit;
        fruit = getOneObjectAtOffset(0, 0, Fruit.class);
        if (fruit != null) //when car contacts fruit, fruit disappears
        {
            World world;
            world = getWorld();
            world.removeObject(fruit);
            eatFruit(); //calling the eatFruit method
        }
    
    } 
    private void eatFruit() //car eats the fruit and score increases
    {
       World newWorld = (MyWorld) getWorld();
       Counter counter = newWorld.getCounter();  //cannot find symbol getCounter(), is it because
       counter.bumpCount(5);                     //the getCounter method is in MyWorld?
    }
    
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class MyWorld extends World
{
    private Counter theCounter;
    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public MyWorld()
    {    
        super(600, 400, 1);
        GreenfootImage bg = new GreenfootImage("space-1.jpg");
        bg.scale(getWidth(), getHeight());
        setBackground(bg);
        prepare();
        theCounter = new Counter();
        addObject(theCounter, 5, 5);
    }
    public Counter getCounter()
    {
        return theCounter;
    }
    private void prepare()
    {
        car car = new car();
        addObject(car,76,50);
        Fruit fruit = new Fruit();
        addObject(fruit,467,74);
        Fruit fruit2 = new Fruit();
        addObject(fruit2,65,297);
        Fruit fruit3 = new Fruit();
        addObject(fruit3,240,206);
        Fruit fruit4 = new Fruit();
        addObject(fruit4,436,301);
        Rocket rocket = new Rocket();
        addObject(rocket,237,342);
        Fruit fruit5 = new Fruit();
        addObject(fruit5,393,200);
        Fruit fruit6 = new Fruit();
        addObject(fruit6,549,209);
        Fruit fruit7 = new Fruit();
        addObject(fruit7,530,359);
        Fruit fruit8 = new Fruit();
        addObject(fruit8,164,137);
        Rocket rocket2 = new Rocket();
        addObject(rocket2,132,237);
    }
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class Counter extends Actor
{
    private int totalCount = 0;

    public Counter()
    {
        setImage(new GreenfootImage("0", 20, Color.WHITE, Color.BLACK));
    }
    public void bumpCount(int amount)
    {
        totalCount += amount;
        setImage(new GreenfootImage("" + totalCount, 20, Color.WHITE, Color.BLACK));
    }     
}
danpost danpost

2018/4/5

#
In car class change World to MyWorld at line 38.
HecticKiwi HecticKiwi

2018/4/7

#
Argh I knew it was something simple like that, I'd tried putting MyWorld in various places in eatFruit() but obviously hadn't tried that one. Thanks a bunch :)
You need to login to post a reply.