Hi everyone, I’m posting here because I’m facing a problem with a “for” loop.
Here is the code that I am working on :
import processing.sound.*;
SoundFile Son;
PImage portrait;
PImage portrait_deux;
PImage telephone;
PImage mail;
PImage arme;
int rand;
int action;
int timer;
int avancement;
int state = 0;
void setup() {
size(1920, 640);
telephone = loadImage("Calls.jpg");
mail = loadImage("Emails.jpg");
arme = loadImage("Kills.jpg");
rand = int(random(0, 155));
timer = millis();
}
void draw() {
for (int avancement = 0; avancement < 10; avancement++) {
if (state == 0) {
takerandomimage("portrait" + nf(rand, 3) + ".jpg");
image (portrait, 0, 0);
if (millis() - timer > 500) {
state=1;
timer=millis();
}
} else if (state == 1) {
image (portrait, 0, 0);
action = int (random (1, 3));
if (action==1) {
image (telephone, 0, 0);
Son = new SoundFile(this, "Son_calls.mp3");
Son.play();
} else if (action==2) {
image (mail, 0, 0);
Son = new SoundFile(this, "Son_emails.mp3");
Son.play();
} else {
println("Error: unknown action: " +action);
}
state = 2;
} else if (state==2) {
if (millis() - timer > 300) {
state=3;
}
} else if (state == 3) {
if (Son!=null) {
if (!Son.isPlaying()) {
rand = int(random(1, 155));
state = 0;
timer=millis();
}
} else {
state = 0;
timer=millis();
}
}
}
if (avancement == 10) {
if (state == 0) {
takerandomimage("portrait" + nf(rand, 3) + ".jpg");
image (portrait, 0, 0);
if (millis() - timer > 500) {
state=1;
timer=millis();
}
} else if (state == 1) {
image (portrait, 0, 0);
image (arme, 640, 0);
Son = new SoundFile(this, "Son_kills.mp3");
Son.play();
state = 2;
} else if (state==2) {
if (millis() - timer > 300) {
state=3;
}
} else if (state == 3) {
if (Son!=null) {
if (!Son.isPlaying()) {
rand = int(random(1, 155));
state = 4;
timer=millis();
}
} else if (state == 4) {
takerandomimage("portrait_deux" + nf(rand, 3) + ".jpg");
image (portrait_deux, 1280, 0);
timer=millis();
}
}
}
}
void takerandomimage(String fn) {
portrait = loadImage(fn);
image(portrait, 0, 0);
portrait_deux = loadImage(fn);
image(portrait_deux, 0, 0);
}
Here is the issue I’m having :
The program runs without stopping although I would like it to stop once the variable “avancement” has reached the value of 10, so that I could run the final part of the code (from line 73 to 103).
In other words :
- While “avancement” < 10
- Run the code from line 29 to 69
- When “avancement” = 10
- Run the code from line 73 to 103
Could someone please highlight the mistakes that I am making ? That would be really helpful.
I hope this makes sense, please tell me if there is something you don’t understand, or something that I forgot to mention.
Anything would help, thank you very much !