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:
And this is the smaller cookie code that will subtract points:
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; } }
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); } } }