Trying to spawn some fish in animation
in
Programming Questions
•
2 years ago
I am trying to have the meeting of two animated fish result in 24 baby each coming onto the screen one at a time every two seconds.
I have tried all day all kinds of things (most if or for or while) and cannot get more than one baby fish to appear.
Here is my code. I have commented the problem code in CAPITAL letters:
// Title: Spawning like fish
// GLOBAL VARIABLES
float FishYLocation;
float maleFishXLocation=1500;
float femaleFishXLocation=1200;
float fishSpeed=2;
float maleFishAcceleration=2;
float fishReproductionSpeed=3;
float babyX;
float babyY;
int time1 = 8000;
int time2 = 2000;
void setup() {
size (1200, 800);
background (230);
frameRate(30);
}
void draw() {
int fishMotion =millis ();
float FishYLocation=height*5/7;
background (230);
// Condition for seduction preceding spawning
if (femaleFishXLocation<maleFishXLocation) {
//Female fish appearing followed by a male fish
if (fishMotion < time1) {
maleFishXLocation-=fishSpeed;
femaleFishXLocation-=fishSpeed;
}
// Male fish showing interest in female fish
else {
maleFishAcceleration*=1.007;
maleFishXLocation-=maleFishAcceleration;
femaleFishXLocation-=fishSpeed;
}
fill(255,40,10);
drawFish (femaleFishXLocation, FishYLocation);
fill(245, 245, 0);
drawFish (maleFishXLocation, FishYLocation);
}
// Fish spawning as they swim off the screen together
else {
femaleFishXLocation-=fishSpeed;
fill(255,40,10);
drawFish (femaleFishXLocation, FishYLocation);
fill(245, 245, 0);
drawFish (femaleFishXLocation-1, FishYLocation);
// Baby fish following behind
fill(255, 180, 0);
scale(0.5);
translate (300, 100);
drawFish (2*femaleFishXLocation, 2*FishYLocation);
// HERE IS WHERE IT DOES NOT WORK
if (fishMotion == time1) {
translate (25, 25);
drawFish (2*femaleFishXLocation, 2*FishYLocation);
}
}
}
// CREATE THE FISH FUNCTION
void drawFish (float fishX, float fishY) {
strokeWeight (3);
beginShape();
vertex(fishX, fishY+50);
vertex(fishX+45, fishY+20);
vertex(fishX+100, fishY+40);
vertex(fishX+150, fishY);
vertex(fishX+120, fishY+90);
vertex(fishX+95, fishY+70);
vertex(fishX+60, fishY+80);
endShape(CLOSE);
arc (fishX+34, fishY+42, 10, 15, 0, PI);
}
I have tried all day all kinds of things (most if or for or while) and cannot get more than one baby fish to appear.
Here is my code. I have commented the problem code in CAPITAL letters:
// Title: Spawning like fish
// GLOBAL VARIABLES
float FishYLocation;
float maleFishXLocation=1500;
float femaleFishXLocation=1200;
float fishSpeed=2;
float maleFishAcceleration=2;
float fishReproductionSpeed=3;
float babyX;
float babyY;
int time1 = 8000;
int time2 = 2000;
void setup() {
size (1200, 800);
background (230);
frameRate(30);
}
void draw() {
int fishMotion =millis ();
float FishYLocation=height*5/7;
background (230);
// Condition for seduction preceding spawning
if (femaleFishXLocation<maleFishXLocation) {
//Female fish appearing followed by a male fish
if (fishMotion < time1) {
maleFishXLocation-=fishSpeed;
femaleFishXLocation-=fishSpeed;
}
// Male fish showing interest in female fish
else {
maleFishAcceleration*=1.007;
maleFishXLocation-=maleFishAcceleration;
femaleFishXLocation-=fishSpeed;
}
fill(255,40,10);
drawFish (femaleFishXLocation, FishYLocation);
fill(245, 245, 0);
drawFish (maleFishXLocation, FishYLocation);
}
// Fish spawning as they swim off the screen together
else {
femaleFishXLocation-=fishSpeed;
fill(255,40,10);
drawFish (femaleFishXLocation, FishYLocation);
fill(245, 245, 0);
drawFish (femaleFishXLocation-1, FishYLocation);
// Baby fish following behind
fill(255, 180, 0);
scale(0.5);
translate (300, 100);
drawFish (2*femaleFishXLocation, 2*FishYLocation);
// HERE IS WHERE IT DOES NOT WORK
if (fishMotion == time1) {
translate (25, 25);
drawFish (2*femaleFishXLocation, 2*FishYLocation);
}
}
}
// CREATE THE FISH FUNCTION
void drawFish (float fishX, float fishY) {
strokeWeight (3);
beginShape();
vertex(fishX, fishY+50);
vertex(fishX+45, fishY+20);
vertex(fishX+100, fishY+40);
vertex(fishX+150, fishY);
vertex(fishX+120, fishY+90);
vertex(fishX+95, fishY+70);
vertex(fishX+60, fishY+80);
endShape(CLOSE);
arc (fishX+34, fishY+42, 10, 15, 0, PI);
}
1