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

2014/3/28

Counter not working? ... - School project

JesperWR JesperWR

2014/3/28

#
I just finished all the codes, but i want a Counter aswell. Anyone know why this does not work? I did it after the mik-blog video. Please help, i dont have much time left! :/ My world code:
    /**
     * Constructor for classen Board
     * 
     */
    public Board()
    {    
        // Laver en ny world med målene 400x500 celler. 1 celle = 1x1 pixel.
        super(400, 500, 1); 

        prepare();
        //Greenfoot.playSound("ironman.mp3");
    }

    /**
     * Forbereder verdenen, tilføjer Drone ved speciel location.
     */
    private void prepare()
    {
        Counter counter = new Counter();
        addObject(counter, 58, 26);
        Drone drone = new Drone();
        addObject(drone, 200, 475);
        Header header = new Header();
        addObject(header, 200, 28);
        Bladtest bladtest = new Bladtest(counter);
        addObject(bladtest, 1, 1);
        
        

    }

    public void act() //Indsæt Bladtest
    {
        if (Greenfoot.getRandomNumber(1000) < 5) {
            addObject(new Bladtest(counter), Greenfoot.getRandomNumber(400), 0);
        }
    }
}
My actor class (when the 'bladtest' his the edge, it should give a point to the counter)
    private Counter counter;
    
    public Bladtest(Counter pointCounter)
    {
        counter = pointCounter;
    }
    
    public void act() 
    {
        int ypos = getY() + 1;
        if (ypos >= getWorld().getHeight()) {
            getWorld().removeObject(this);
        }
        else {
            setLocation(getX(), ypos);
            
        }
        
        atWorldEdge();
        checkEdge();
    }
    
    
    /**
     * Boolean om Obstacle class er ved Y-koordinaten 499 (nederste væg)
     */
    public boolean atWorldEdge()  
    {  
        if(getY() >= 499)
            return true;  
        else  
            return false;
    }
    
    public void checkEdge()
    {
        if (this.atWorldEdge() == true)
        {
            World world;
            world = getWorld();
            world.removeObject(this);
            counter.add(1);
        }
    }
}
danpost danpost

2014/3/28

#
The problem is that in line 19 of your Board class, you are creating a new variable 'counter' (which is not the same as your instance 'counter' field. Remove 'Counter' from the beginning of that line so that 'counter' in that line will refer to your instance field. That should fix it (or at least help to).
You need to login to post a reply.