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

2013/11/18

Need help with score counter

LightOfDivine LightOfDivine

2013/11/18

#
So I've followed this apparently quick guide on how to make a score counter, but it doesn't seem to add up, it doesn't give any errors either and as I'm really new to this I've got no idea what's wrong at this point, I used this guide btw: http://computingteacher.edublogs.org/files/2013/02/GreenfootScores-vvyjld.pdf however instead of Turtle I ofcourse used Butterfly as my game doesn't have a turtle.
import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot)
import java.awt.Color;
import java.awt.Graphics;

public class FlowerCounter extends Actor
{
    private static final Color TEXT_COLOR = new Color(200, 0, 0);
    private static final Color TRANSPARENT_COLOR = new Color(0, 0, 0, 0);

    private Butterfly butterfly;

    public FlowerCounter(Butterfly butterfly)
    {
        this.butterfly = butterfly;

        updateImage();
    }

    public void act() {
        updateImage();
    }

    private void updateImage()
    {
        String text = "Flowers Nibbled: " + butterfly.getScore();
        GreenfootImage image = new GreenfootImage(text, 20, TEXT_COLOR, 
                TRANSPARENT_COLOR);
        setImage(image);
    }
} 
And here the code that's in my Butterfly which should add it, it also contains the code for the flower to respawn.
public void act()
    {
        moveButterfly();
        nibbleFlower();
        getScore();
    }
public void nibbleFlower()
    {
        Actor flower;
        flower =  getOneObjectAtOffset(0, 0, Flower.class);
        if (canSee (Flower.class) )
        {
            flowersNibbled++;
            World world;
            world = getWorld();
            world.removeObject(flower);
            Greenfoot.playSound("NomNomNom2.wav");
            getWorld().addObject(new Flower(), Greenfoot.getRandomNumber(getWorld().getWidth()), Greenfoot.getRandomNumber(getWorld().getHeight()));
          }
    }
    
    public int getScore()
    {
        return flowersNibbled;
    }
danpost danpost

2013/11/18

#
The problem does not appear to be in the class provided. Let us see the Butterfly class please. BTW, there is no need for line 3 (import java.awt.Graphics;). EDIT: I see you edited your post and added extra code.
danpost danpost

2013/11/18

#
Please explain what you mean by 'it doesn't seem to add up'. Explain what behavior you are getting.
LightOfDivine LightOfDivine

2013/11/18

#
danpost wrote...
Please explain what you mean by 'it doesn't seem to add up'. Explain what behavior you are getting.
Basically the score start at this (forgot to add this line in the first post, sorry for that by the way);
public int flowersNibbled=-1;
It's above the Public Void act, and it puts the score as default on -1 (-1 as my butterfly spawns on a Flower) but whenever I pick up a flower it doesn't count up, neither does it when I make it start at 0 or any other number. So it doesn't show my total flower count.
danpost danpost

2013/11/18

#
Maybe we should take a look at the world class where you create the objects (please include any code that could possibly be needed). Code related to butterfly objects, flower objects, and the flowerCounter object.
LightOfDivine LightOfDivine

2013/11/18

#
danpost wrote...
Maybe we should take a look at the world class where you create the objects (please include any code that could possibly be needed).
Do I just paste in the whole thing? As it's quite a long list due to 'private void prepare', beside that there's nothing really in there beside this;
import greenfoot.*;  // imports Actor, World, Greenfoot, GreenfootImage

public class StevenWorld extends World
{
    /**
     * Create the turtle world. Our world has a size 
     * of 560x460 cells, where every cell is just 1 pixel.
     */
    public StevenWorld() 
    {
        super(600, 480, 1);
        prepare();
        act();
    }
    
    public void act()
    {
        if (Greenfoot.isKeyDown("enter")) 
        {
            Greenfoot.setWorld(new StevenWorld());
        }
    }
Below this is the whole Prepare list, which also contains all of the things I removed from the map at certain points.
danpost danpost

2013/11/18

#
Yes, you will have to post the whole thing. I have a feeling that the butterfly object passed to the flowerCounter class is not the same one as that which is placed in the world.
LightOfDivine LightOfDivine

2013/11/18

#
danpost wrote...
Yes, you will have to post the whole thing. I have a feeling that the butterfly object passed to the flowerCounter class is not the same one as that which is placed in the world.
Here goes :)
import greenfoot.*;  // imports Actor, World, Greenfoot, GreenfootImage

public class StevenWorld extends World
{
    public StevenWorld() 
    {
        super(600, 480, 1);
        prepare();
        act();
    }
    
    public void act()
    {
        if (Greenfoot.isKeyDown("enter")) 
        {
            Greenfoot.setWorld(new StevenWorld());
        }
    }

    /**
     * Prepare the world for the start of the program. That is: create the initial
     * objects and add them to the world.
     */
    private void prepare()
    {
        Flower flower = new Flower();
        addObject(flower, 513, 375);
        Flower flower2 = new Flower();
        addObject(flower2, 294, 432);
        Flower flower3 = new Flower();
        addObject(flower3, 80, 400);
        Flower flower4 = new Flower();
        addObject(flower4, 266, 324);
        Flower flower5 = new Flower();
        addObject(flower5, 74, 272);
        Flower flower6 = new Flower();
        addObject(flower6, 137, 180);
        Flower flower7 = new Flower();
        addObject(flower7, 58, 47);
        Flower flower8 = new Flower();
        addObject(flower8, 241, 18);
        Flower flower9 = new Flower();
        addObject(flower9, 529, 174);
        Flower flower10 = new Flower();
        addObject(flower10, 430, 79);
        Flower flower11 = new Flower();
        addObject(flower11, 535, 48);
        Flower flower12 = new Flower();
        addObject(flower12, 238, 108);
        Butterfly butterfly = new Butterfly();
        addObject(butterfly, 348, 226);
        Frog frog = new Frog();
        addObject(frog, 111, 35);
        Frog frog2 = new Frog();
        addObject(frog2, 571, 445);
        butterfly.setLocation(299, 236);
        frog.setLocation(30, 26);
        frog2.setLocation(572, 451);
        flower4.setLocation(270, 339);
        removeObject(flower4);
        removeObject(flower2);
        removeObject(flower);
        removeObject(flower3);
        removeObject(flower5);
        removeObject(flower6);
        removeObject(flower7);
        removeObject(flower12);
        removeObject(flower8);
        removeObject(flower10);
        removeObject(flower11);
        flower9.setLocation(544, 53);
        frog2.setLocation(572, 451);
        frog2.setLocation(572, 451);
        frog2.setLocation(572, 451);
        frog2.setLocation(597, 515);
        frog2.setLocation(813, 810);
        butterfly.setLocation(439, 400);
        Frog frog3 = new Frog();
        addObject(frog3, 813, 51);
        Frog frog4 = new Frog();
        addObject(frog4, 47, 817);
        flower9.setLocation(567, 75);
        flower9.setLocation(435, 399);
        Frog frog5 = new Frog();
        addObject(frog5, 797, 410);
        Frog frog6 = new Frog();
        addObject(frog6, 46, 411);
        Grass grass = new Grass();
        addObject(grass, 201, 331);
        Grass grass2 = new Grass();
        addObject(grass2, 367, 137);
        Grass grass3 = new Grass();
        addObject(grass3, 655, 280);
        Grass grass4 = new Grass();
        addObject(grass4, 484, 697);
        Grass grass5 = new Grass();
        addObject(grass5, 714, 584);
        Grass grass6 = new Grass();
        addObject(grass6, 496, 425);
        Grass grass7 = new Grass();
        addObject(grass7, 113, 677);
        Grass grass8 = new Grass();
        addObject(grass8, 362, 782);
        Grass grass9 = new Grass();
        addObject(grass9, 243, 505);
        Grass grass10 = new Grass();
        addObject(grass10, 67, 200);
        Grass grass11 = new Grass();
        addObject(grass11, 179, 86);
        Grass grass12 = new Grass();
        addObject(grass12, 611, 77);
        Grass grass13 = new Grass();
        addObject(grass13, 777, 202);
        grass6.setLocation(507, 434);
        butterfly.setLocation(438, 397);
        butterfly.setLocation(441, 393);
        grass3.setLocation(635, 315);
        grass3.setLocation(650, 312);
        butterfly.setLocation(440, 393);
        butterfly.setLocation(434, 398);
        butterfly.setLocation(423, 409);
        removeObject(butterfly);
        removeObject(flower9);
        removeObject(frog6);
        removeObject(frog4);
        removeObject(frog2);
        removeObject(frog5);
        removeObject(frog3);
        removeObject(frog);
        Flower flower13 = new Flower();
        addObject(flower13, 464, 403);
        Frog frog7 = new Frog();
        addObject(frog7, 49, 37);
        Frog frog8 = new Frog();
        addObject(frog8, 815, 41);
        Frog frog9 = new Frog();
        addObject(frog9, 813, 814);
        Frog frog10 = new Frog();
        addObject(frog10, 40, 822);
        Frog frog11 = new Frog();
        addObject(frog11, 45, 412);
        Frog frog12 = new Frog();
        addObject(frog12, 809, 415);
        Butterfly butterfly2 = new Butterfly();
        addObject(butterfly2, 472, 408);
        Rock rock = new Rock();
        addObject(rock, 245, 415);
        Rock rock2 = new Rock();
        addObject(rock2, 456, 180);
        Rock rock3 = new Rock();
        addObject(rock3, 693, 416);
        Rock rock4 = new Rock();
        addObject(rock4, 285, 693);
        Rock rock5 = new Rock();
        addObject(rock5, 658, 743);
        Rock rock6 = new Rock();
        addObject(rock6, 188, 193);
        removeObject(rock);
        removeObject(rock6);
        removeObject(rock2);
        removeObject(rock3);
        removeObject(rock5);
        removeObject(rock4);
        butterfly2.setLocation(426, 406);
        flower13.setLocation(419, 400);
        FlowerCounter flowercounter = new FlowerCounter(butterfly);
        addObject(flowercounter, 590, 782);
        flowercounter.setLocation(424, 13);
        grass7.setLocation(116, 420);
        grass9.setLocation(266, 436);
        butterfly2.setLocation(304, 318);
        flower13.setLocation(295, 255);
        butterfly2.setLocation(299, 254);
        frog12.setLocation(491, 300);
        removeObject(frog12);
        frog9.setLocation(451, 280);
        removeObject(frog9);
        frog8.setLocation(485, 51);
        removeObject(frog8);
        grass6.setLocation(475, 322);
        grass8.setLocation(372, 413);
        grass4.setLocation(482, 201);
        grass13.setLocation(599, 202);
        grass13.setLocation(476, 84);
        grass12.setLocation(275, 215);
        grass3.setLocation(575, 207);
        grass5.setLocation(561, 416);
        frog11.setLocation(567, 28);
        frog10.setLocation(35, 445);
        flowercounter.setLocation(299, 15);
    }
}
danpost danpost

2013/11/18

#
Line 122 removes 'butterfly' from the world and line 144 creates a 'butterfly2' object which is the one that ends up in your world. Line 166 passes the removed butterfly to the flowerCounter object; and since that butterfly is not in the world, it never nibbles any flowers. Change 'butterfly' in line 166 to 'butterfly2'.
LightOfDivine LightOfDivine

2013/11/18

#
danpost wrote...
Line 122 removes 'butterfly' from the world and line 144 creates a 'butterfly2' object which is the one that ends up in your world. Line 166 passes the removed butterfly to the flowerCounter object; and since that butterfly is not in the world, it never nibbles any flowers. Change 'butterfly' in line 166 to 'butterfly2'.
It works, thank you very much :)
Baris Baris

2015/1/12

#
Baris wrote...
Can someone help me?, I cannt get my Counter to work.
I have added the code below in Counter (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,20, Color.BLUE, Color.WHITE));
    }

    public void addScore()
    {
        score++;
    }
I have added the code below in Level1 (World)
    Counter counter = new Counter();
    /**
     * Constructor for objects of class Level1.
     * 
     */
    public Level1()
    {    
        super(600, 400, 1); 
        prepare(); //Names and Locations of all the Actors in Level1
    }

    public Counter getCounter()
    {
        return counter;
    }
You need to login to post a reply.