Let’s say that we have a simple class in use here:
boolean wheatClicked = false;
boolean carrotClicked = false;
boolean potatoClicked = false;
class Crop
{
int quantity;
Crop(int cropQuantity)
{
quantity = cropQuantity;
}
void IncrementData()
{
quantity += 1;
}
wheatClicked = false;
carrotClicked = false;
potatoClicked = false;
}
Crop wheat;
Crop carrot;
Crop potato;
void setup()
{
wheat = new Crop(1);
carrot = new Crop(5);
potato = new Crop(10);
}
void draw()
{
if (wheatClicked)
wheat.IncrementData();
if (carrotClicked)
carrot.IncrementData();
if (potatoClicked)
potato.IncrementData();
}
void mousePressed()
{
if (mouseX > 100 && mouseX < 150)
{
if (mouseY > 250 && mouseY < 300)
wheatClicked = true;
if (mouseY > 350 && mouseY < 400)
carrotClicked = true;
if (mouseY > 450 && mouseY < 500)
potatoClicked = true;
}
}
However, there won’t be just 3 crops, there will be dozens. So do I have to physically write out dozens of dozens of lines of repetitive code? Or can I loop these in perhaps a for loop
or something?
And if so, how would I do so?
Thanks