Hello guys.
I’ve bee fighting a lot now to get the objects spawn right. The game have 3 elements spawning but the problem is to make them spawn after an object is pressed. I tried to use boolean but it won’t work and will just spawn all the elements at same time then. The timer works, but it already starts from the beginning while i try to make it start after i’ve clicked the “continue” button.
My script is a bit janky since i am new, and i am doing this whole project myself even though we are 6 people working on it.
My main page:
AlgaeGroup[] algae;
SinkingObject[] plasticPieces;
SunscreenDrop[] sunscreen = new SunscreenDrop [1];
SwimmingFishes fishes;
CrabMayor crab;
PlankPoints pPoints;
Timer timer;
float xDrop = random(-5);
float yDrop = 350 + random(5);
float dropRadius = 15;
//Pictures
PImage crs1Img;
PImage crs2Img;
PImage crs3Img;
PImage crs4Img;
PImage crs5Img;
PImage plasticImg;
PImage algaeImg;
PImage floaterImg;
PImage crabMayorImg;
PImage plankImg;
PImage fishEyeRightImg;
PImage fishEyeLeftImg;
PImage speechBubble0Img;
PImage continueButtonImg;
PFont pointsFont;
int points = 0;
boolean stage1;
boolean stage2;
boolean stage3;
boolean stage4;
boolean crabGone = false;
int timeIntervalS1Plastics;
int activePlastics;
int plasticsTotal = 50;
boolean plasticsS1;
int timeIntervalS1Algae;
int activeAlgae;
int algaeTotal = 15;
boolean algaeS1;
void setup() {
size(1280, 720);
noStroke();
frameRate(60);
crs1Img = loadImage("coralReefStage1.png");
crs2Img = loadImage("coralReefStage2.png");
crs3Img = loadImage("coralReefStage3.png");
crs4Img = loadImage("coralReefStage4.png");
crs5Img = loadImage("coralReefStage5.png");
plasticImg = loadImage("flaske.png");
algaeImg = loadImage("algae.png");
floaterImg = loadImage("floater.png");
crabMayorImg = loadImage("crabMayor.png");
plankImg = loadImage("plank.png");
fishEyeRightImg = loadImage("eyeRight.png");
fishEyeLeftImg = loadImage("eyeLeft.png");
speechBubble0Img = loadImage("speechBubble0.png");
continueButtonImg = loadImage("continueButton.png");
pointsFont = createFont("almondNougat.ttf", 40);
fishes = new SwimmingFishes();
crab = new CrabMayor();
pPoints = new PlankPoints();
plasticPieces = new SinkingObject[plasticsTotal];
for (int i = 0; i < plasticsTotal; i++) {
plasticPieces[i] = new SinkingObject();
}
algae = new AlgaeGroup[algaeTotal];
for (int i = 0; i < algaeTotal; i++) {
algae[i] = new AlgaeGroup();
}
for (int i = 0; i < sunscreen.length; i++) {
sunscreen[i] = new SunscreenDrop(xDrop, yDrop, dropRadius);
}
//Stages and timer
stage1 = points <= 9;
stage2 = points >= 10 && points <= 19;
stage3 = points >= 20 && points <= 29;
stage4 = points >= 30 && points <= 39;
activePlastics = 0;
activeAlgae = 0;
//crab.crabIsGone();
if (stage1) {
timeIntervalS1Plastics = 2000;
timeIntervalS1Algae = 3000;
timer = new Timer(timeIntervalS1Plastics);
timer = new Timer(timeIntervalS1Algae);
timer.start();
} else if (stage2) {
}
}
void draw () {
for (SunscreenDrop sunscreen : sunscreen)
sunscreen.moveDrop();
stage1 = points <= 9;
stage2 = points >= 10 && points <= 19;
stage3 = points >= 20 && points <= 29;
stage4 = points >= 30 && points <= 39;
if (stage1) {
background(crs1Img);
} else if (stage2) {
background(crs2Img);
} else if (stage3) {
background(crs3Img);
} else if (stage4) {
background(crs4Img);
} else {
background(crs5Img);
}
fishes.display();
for (SunscreenDrop sunscreen : sunscreen)
sunscreen.dropDisplay();
for (SunscreenDrop sunscreen : sunscreen)
sunscreen.manDisplayAndSpeed();
for (SunscreenDrop sunscreen : sunscreen)
sunscreen.disappearBottom();
for (SunscreenDrop sunscreen : sunscreen)
sunscreen.disappearOnClick();
if (stage1) {
crab.display();
crab.speaking0();
} else if (stage2) {
}
algaeS1 = activeAlgae < algaeTotal - 12;
if (timer.completeS1Algae() == true) {
if (algaeS1) {
activeAlgae++;
timer.start();
}
}
plasticsS1 = activePlastics < plasticsTotal - 43;
if (timer.completeS1Plastics() == true) {
if (plasticsS1) {
activePlastics++;
timer.start();
}
}
/*plasticsS1 = activePlastics < plasticsTotal - 43;
if (timer.completeS1Algae() == true || timer.completeS1Plastics() == true){
if (algaeS1 || plasticsS1) {
activeAlgae++;
activePlastics++;
timer.start();
}*/
for (int i = 0; i < activeAlgae; i++) {
algae[i].display();
algae[i].grow();
algae[i].overGrowth();
algae[i].disappearOnClick();
}
for (int i = 0; i < activePlastics; i++) {
plasticPieces[i].display();
plasticPieces[i].move();
plasticPieces[i].disappearBottom();
plasticPieces[i].disappearOnClick();
}
pPoints.display();
/* fill(#000000);
stroke(200);
line(0, 550, width, 550);*/
}
void mouseReleased() {
}
the 2 other important classes are:
class CrabMayor {
float xCrab = width-300, yCrab = height-300;
float crabSize = 300;
float xBubble = xCrab - 650, yBubble = yCrab - 200;
float lBubble = 700, hBubble = 400;
float xContinue = xBubble * 2, yContinue = yBubble * 2.2;
float lContinue = 200, hContinue = 50;
int outOfScreen = 2000;
boolean overButton = false;
boolean condition1;
boolean condition2;
boolean condition3;
boolean condition4;
void display() {
if (stage1) {
crabGone = false;
image(crabMayorImg, xCrab, yCrab, crabSize, crabSize);
}
}
void speaking0() {
image(speechBubble0Img, xBubble, yBubble, lBubble, hBubble);
image(continueButtonImg, xContinue, yContinue, lContinue, hContinue);
condition1 = mouseX > xContinue;
condition2 = mouseX < (xContinue + lContinue);
condition3 =(mouseY < yContinue + hContinue);
condition4 = (mouseY > yContinue);
if (condition1 && condition2 && condition3 && condition4) {
cursor(HAND);
overButton = true;
} else {
cursor(ARROW);
overButton = false;
}
if (mousePressed) {
if (overButton == true) {
xCrab = outOfScreen;
xBubble = outOfScreen;
xContinue = outOfScreen;
}
}
if (xCrab == outOfScreen) {
crabGone = true;
}
}
/*void crabIsGone() {
if (xCrab == outOfScreen) {
crabGone = true;
}
}*/
}
and
class Timer {
int startTimeS1Plastics;
int intervalS1Plastics;
int startTimeS1Algae;
int intervalS1Algae;
Timer(int timeInterval) {
intervalS1Plastics = timeInterval;
intervalS1Algae = timeInterval;
}
boolean completeS1Plastics() {
int elapsedTime = millis() - startTimeS1Plastics;
if (elapsedTime > intervalS1Plastics) {
return true;
} else {
return false;
}
}
boolean completeS1Algae() {
int elapsedTime = millis() - startTimeS1Algae;
if (elapsedTime > intervalS1Algae) {
return true;
} else {
return false;
}
}
void start() {
if (crabGone == true && stage1){
startTimeS1Plastics = millis();
startTimeS1Algae = millis();
}
}
}
I have tried using this:
void start() {
if (crabGone == true && stage1){
startTimeS1Plastics = millis();
startTimeS1Algae = millis();
}
}
but then it spawn everything at same time. if i remove the if statement and just write the startTime variables it works fine, but it begins from the start and not when the statements are true. I am not 100% sure of what the void start() does. I have 5 stages but i just need to get the first done, and then i think i can get the others implemented as well… The other classes are just other objects, which should spawn, but write if they are needed in order to make you understand and try it yourself, i don’t include them since it would make my post much longer.
Oh yeah, and btw. Please explain if you rewrite code or just write code, and sorry for not using capital I every time.