I'm not sure if this is an API problem, but whenever I create a temporary object, and that object is removed by clicking, an issue occurs. It disappears; however, the variable I have to check the amount of times the object has been clicked increases by 4 and not 1 because the object is being clicked 4 times in the span it takes to check a click and remove the object.MyWorld Class^^^Block class^^^
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class MyWorld here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class MyWorld extends World
{
public static block currentBlock;
public static int Click = 0;
public static int Miss = 0;
static int Accuracy = 1;
static int TotalClicked = 0;
/**
* Constructor for objects of class MyWorld.
*
*/
public MyWorld()
{
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
super (600, 400, 1);
addObject( new block(), Greenfoot.getRandomNumber(600), Greenfoot.getRandomNumber(400));
addObject( new block(), Greenfoot.getRandomNumber(600), Greenfoot.getRandomNumber(400));
addObject( new block(), Greenfoot.getRandomNumber(600), Greenfoot.getRandomNumber(400));
System.out.println("This window will keep record of your clicks and your accuracy");
}
public static void calcTotal()
{
TotalClicked = TotalClicked + 1;
UpdateTotal();
}
public static void calcAcc()
{
if (((Click + Miss)/4) >= 1)
{
Accuracy = (Click / Miss) * 100;
UpdateAcc();
}
}
public static void UpdateAcc()
{
System.out.println(Accuracy);
System.out.println("^Current Accuracy (%)");
}
public static void UpdateTotal()
{
System.out.println(TotalClicked);
System.out.println("^Current Clicks ");
}
public void showMiss()
{
System.out.println(Miss);
}
public void showHit()
{
System.out.println(Click);
}
public void showTotalClicked()
{
System.out.println(TotalClicked);
}
public void showAccuracy()
{
System.out.println(Accuracy);
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class block here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class block extends Actor
{
static int i = 0;
static int o = 0;
public void act()
{
MyWorld.currentBlock = this;
Start();
}
public void Start()
{
if(Greenfoot.mousePressed(this))
{
if(o == 0)
{
getWorld().addObject( new block(), Greenfoot.getRandomNumber(600), Greenfoot.getRandomNumber(400));
getWorld().removeObject(this);
Hit();
}
}
if(Greenfoot.mousePressed(null))
{
Miss();
}
}
public void Hit()
{
MyWorld.Click = MyWorld.Click + 1;
MyWorld.calcTotal();
MyWorld.calcAcc();
}
public void Miss()
{
MyWorld.Miss = MyWorld.Miss + 1;
MyWorld.calcTotal();
MyWorld.calcAcc();
}
/**
* DEBUGGING TOOL
*
* -A method to probe for the location of loops and multiple occurances-
*
* Place "i = i + 1" to discover the amount of repetitions or areas connected
* to the forever loop.
*/
public void showI()
{
System.out.println(i);
}
}

