Hello, for school we have to make our own game so I have decided to recreate boxworld. This is the code where I have a problem
(The images are not set yet so I am using images from Greenfoot)
I want that when the box is on the right place amountOfBoxes goes up by one one time. Now when a box is on the right place amountOfBoxes goes up by one, but it doesn't stop going up by one, it keeps going up by one. How can I change it so that when a box is on a right place amountOfBoxes only goes up by one?
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class box here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Box extends Actor
{
private int amountOfBoxes=0;
private long startTijd = System.currentTimeMillis();
private GreenfootImage image1 = new GreenfootImage("apple.png");
private GreenfootImage image2 = new GreenfootImage("ball.png");
/**
* Act - do whatever the box wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
if(rightPlace()){
changeBox();
amountOfBoxes++;
}
if(rightPlace()==false&&getImage()==image1)
{
changeBox2();
if(amountOfBoxes<0)
amountOfBoxes--;
}
if(getAmountOfBoxes()==4){
long tijd = System.currentTimeMillis();
long tijdsduur = (tijd - startTijd) / 1000;
String message= "winner winner chicken dinner" + " time: " + tijdsduur + " seconds";
getWorld().showText(message, getWorld().getWidth()/2, getWorld().getHeight()/2);
Greenfoot.stop();
}
}
/**
* checks if the box is in the right place (on a point)
*/
public Boolean rightPlace()
{
Actor actor = getOneObjectAtOffset(0,0,Point.class);
return actor != null;
}
/**
* returns how many boxes you have placed on the right place
*/
public int getAmountOfBoxes()
{
return amountOfBoxes;
}
/**
* changes the box to indicate that it is in the right place
*/
public void changeBox()
{
Point point = (Point) getOneObjectAtOffset(0,0,Point.class);
if(point!= null)
setImage("apple.png");
}
public void changeBox2()
{
setImage("ball.png");
}
}
