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

2017/2/12

Making a Score Counter?

beshie beshie

2017/2/12

#
Hey! So I've read a lot of discussions regarding this already but I just can't seem to get it right. I'm already able to show the "Score: 0" text on the screen but whenever my actor hits a coin, it doesn't update. I want to add a point to the score whenever my actor (it's a bear) gets in contact with a coin. What are the steps to make this happen? Thank you so much!
danpost danpost

2017/2/12

#
beshie wrote...
Hey! So I've read a lot of discussions regarding this already but I just can't seem to get it right. I'm already able to show the "Score: 0" text on the screen but whenever my actor hits a coin, it doesn't update. I want to add a point to the score whenever my actor (it's a bear) gets in contact with a coin. What are the steps to make this happen? Thank you so much!
Show your code where you have it show 'Score: 0". Show the relevant class codes.
Nosson1459 Nosson1459

2017/2/12

#
You need a score variable, whenever you want to increase your score increase the amount for this variable then to display the score do ' "Score: " + scoreVariable '
beshie beshie

2017/2/13

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

/**
 * Write a description of class Game here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Game extends World
{
    Counter counter = new Counter();
    /**
     * Constructor for objects of class Game.
     * 
     */
    public Game()
    {    
        // Create a new world with 600x450 cells with a cell size of 1x1 pixels.
        super(600, 450, 1, false);
        pause();
        generateIce();
        prepare();
        getCounter();
    }

    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        Button pause = new Button();
        Bailey bailey = new Bailey();
        Ice ice = new Ice();
        addObject(pause, 28, 32);
        addObject(counter, 540, 25);
        addObject(ice, 55, 350);
        addObject(bailey, 70, 300);
    }
    
    public void pause()
    {
        if(Greenfoot.isKeyDown("P"))
        {
            Greenfoot.setWorld(new Pause());
        }
    }

    public void generateIce()
    {
        if(Greenfoot.getRandomNumber(100) < 3)
        {
            addObject(new Ice(), 599, Greenfoot.getRandomNumber(450));
        }
    }
    
    public Counter getCounter()
    {
        return counter;
    }
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import greenfoot.Color;
/**
 * Write a description of class Bailey here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Bailey extends Actor
{
    int dy = 0;
    int g = 2;
    /**
     * Act - do whatever the Bailey wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        lose();
        move();
        setLocation(getX(), getY() + dy);
        dy = dy + g;
    }   
    
    public void lose()
    {
        if (isAtEdge())
        {
            Greenfoot.setWorld(new Lose());
        }
    }
    
    public void move()
    {
        if (Greenfoot.isKeyDown("up"))
        {
            dy = -5;
        }
        
        if (Greenfoot.isKeyDown("right"))
        {
            move(5);
        }
        
        if (Greenfoot.isKeyDown("left"))
        {
            move(-5);
        }
    }
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Coin here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Coin extends Actor
{
    /**
     * Act - do whatever the Coin wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        Actor bailey = getOneIntersectingObject(Bailey.class);
        if (bailey!=null)
        {
          Counter counter = new Counter();
          counter.addScore(); 
          getWorld().removeObject(this);
        }
    }
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import greenfoot.Color;
/**
 * Write a description of class Counter here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Counter extends Actor
{
    public int score = 0;
    /**
     * updateImage - do whatever the Counter wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void updateImage() 
    {
        setImage(new GreenfootImage("Score: " + score, 25, Color.WHITE, new Color(0,0,0,0)));
    }    
    
    public Counter()
    {
        updateImage();
    }
    
    public void addScore()
    {
        score++;
        updateImage();
    }
}
beshie beshie

2017/2/13

#
The codes I posted (in order) are the Game world, the main actor Bailey, the item Coin, and the Counter class.
Nosson1459 Nosson1459

2017/2/13

#
In Coin you should use the getCounter() method from Game to add your score to.
danpost danpost

2017/2/13

#
If game is your first world, then I do not believe that the 'pause' method will work in the constructor (line 20). Does that actually do what you want it to do? Line 23 in the constructor of the Game class does not accomplish anything. A counter object is returned, but you are not doing anything to it or using it in some way. Line 20 of the Coin class is creating a new distinctly different Counter object from the one that is already in your world. Adding to it and removing the coin from the world does not do anything for the counter that is in your world. To add to the one that is in your world, do this:
((Game)getWorld()).getCounter().addScore();
(replacing lines 20 and 21 of the Coin class).
Nosson1459 Nosson1459

2017/2/13

#
Which is what I basically said to do/use.
beshie beshie

2017/2/13

#
I finally got it! Thank you so much, guys! You literally saved my grade. I deleted the 'pause()' from the constructor in the Game world and changed the method itself to 'act()', and replaced lines 20 and 21 as said. By the way, how do you make the actor only jump twice and after the second one, fall? I'm also trying to make platforms out of icecaps. How do I do it? And is there a way wherein I can generate icecaps one by one at a steady pace and within a certain range? There are also supposed to be coins on every icecap but I don't know how to do it?
danpost danpost

2017/2/13

#
Please follow the written guidelines found here when asking for programming help.
beshie beshie

2017/2/13

#
danpost wrote...
Please follow the written guidelines found here when asking for programming help.
Okay, sorry!
You need to login to post a reply.