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

2017/5/23

Counter isn't counting!

johnmir19 johnmir19

2017/5/23

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
 * Write a description of class Counter here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Counter extends Actor
{
    int score = 0;
    /**
     * Act - do whatever the Counter wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        setImage(new GreenfootImage("Score : " + score, 24, Color.GREEN, Color.BLACK));
        //World myWorld = getWorld();
        //Space space = (Space)myWorld;
        //Actor cherries = getOneIntersectingObject(Cherries.class);
        //if (cherries != null)
        //{
            //addScore();
        //}
        
    }
    
    public void addScore()
    {
       score++;
    }
}
This is the code that I have in my counter actor and according to the video lessons I have watched, all my code in this class and others are correct, yet when I shoot the (bananas) with the (cherries), even though they both intersect and disappear, the score does not change at all. How do I fix this?
danpost danpost

2017/5/23

#
Please show your World subclass (Space class) code.
johnmir19 johnmir19

2017/5/25

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Space here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Space extends World
{
    Counter counter = new Counter();
    /**
     * 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); 
        prepare();
    }

    public Counter getCounter()
    {
        return counter;
    }
    
    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        PieShip pieship = new PieShip();
        addObject(pieship, getWidth()/2, getHeight()/2);
        Bananas bananas = new Bananas();
        addObject(bananas,Greenfoot.getRandomNumber(getWidth()), (Greenfoot.getRandomNumber(getHeight())));
        //if(getWorld().getObjectsAt(getX(), getY()).isEmpty())
        Bananas bananas2 = new Bananas();
        addObject(bananas2,Greenfoot.getRandomNumber(getWidth()), (Greenfoot.getRandomNumber(getHeight())));
        Bananas bananas3 = new Bananas();
        addObject(bananas3,Greenfoot.getRandomNumber(getWidth()), (Greenfoot.getRandomNumber(getHeight())));
        Bananas bananas4 = new Bananas();
        addObject(bananas4,Greenfoot.getRandomNumber(getWidth()), (Greenfoot.getRandomNumber(getHeight())));
        Bananas bananas5 = new Bananas();
        addObject(bananas5,Greenfoot.getRandomNumber(getWidth()), (Greenfoot.getRandomNumber(getHeight())));
        Counter counter = new Counter();
        addObject(counter,100, 40);
    }
}
danpost danpost

2017/5/25

#
Line 47 is causing the problem. You are creating a new local variable to hold a Counter object and adding that object into the world instead of adding the one already held by the Counter field declared at line 11. Remove line 47.
johnmir19 johnmir19

2017/5/26

#
Thank you! I really appreciate it! I'll let you know if it works!
You need to login to post a reply.