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

2018/11/30

Counter Problems

MemThef MemThef

2018/11/30

#
I'm trying to make a cookie clicker game and made it so that when you hit one of the smaller cookies, it subtracts points from your score. I was wondering if its possible to prevent it from going below zero and just stop there instead. This is my counter code:
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)

import java.awt.Graphics;

/**
 * Counter that displays a number with a text prefix.
 * 
 * @author Michael Kolling
 * @version 1.0.2
 */
public class Counter extends Actor
{
    private static final Color textColor = new Color(255, 180, 150);
    
    private int value = 0;
    private int target = 0;
    private String text;
    private int stringLength;
    private int totalCount = 100;

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

    public void updateImage()
    {
        setImage(new GreenfootImage("" + value, 20, Color.WHITE, Color.BLUE));
    }
  
    public void act() {
        if(value < target) {
            value++;
            updateImage();
        }
        else if(value > target) {
            value--;
            updateImage();
        }
    }

    public void add(int score)
    {
        target += score;
    }

    public void subtract(int score)
    {
        target -= score;
    }

    public int getValue()
    {
        return value;
    }

}
And this is the smaller cookie code that will subtract points:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class smallCookie here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class smallCookie extends Actor
{
    public smallCookie()  
    {  
        GreenfootImage image = getImage();  
        image.scale(image.getWidth() - 425, image.getHeight() - 425);  
        setImage(image); 
        turn(90);
    }
    /**
     * Act - do whatever the smallCookie wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        move(4);
        if(getY()>=598){
            getWorld().removeObject(this);
        }
        if (Greenfoot.mouseClicked(this)==true)
        {

              BackGround theWorld = (BackGround) getWorld();  // get a reference to the world
             Counter counter = theWorld.getCounter();  // get a reference to the counter
              counter.add(-5);
                }
    }
}
danpost danpost

2018/11/30

#
MemThef wrote...
I'm trying to make a cookie clicker game and made it so that when you hit one of the smaller cookies, it subtracts points from your score. I was wondering if its possible to prevent it from going below zero and just stop there instead. << Code Omitted >>
Interesting situation -- seems impossible to control from outside this particular Counter class because its getValue method does not return the target score, which is truly the real value of the counter. So, change line 58 in the Counter class to:
return target;
Then change line 33 in your smallCookie class to:
counter.subtract(Math.min(5, counter.getValue()));
MemThef MemThef

2018/12/3

#
Thanks a lot for the help! It worked perfectly.
You need to login to post a reply.