Pause and resume
in
Programming Questions
•
5 months ago
Hi, guys! I've been making a game and I wanna add pause function to it. But unfortunately there was a bug.
Here's a simplified version of my code.Each second an ellipse falls from the top of the screen.
UPDATE: If I press a key and pause the sketch, when I resume it - there are lots of circles in the top of the screen. How to get rid of that, so that after I resume it, everything is as it was before pause?
- myClass[] myObject = new myClass[40];
- boolean paused=false;
- void setup() {
- size(200, 400);
- smooth();
- for (int i=0; i<40; i++) {
- myObject[i]=new myClass();
- }
- }
- int t=0;
- void draw() {
- background(0);
- if (!paused) {
- for (t=0; millis()>t; t+=1000) {
- myObject[t/1000].appear();
- }
- }else{t=millis();}
- }
- void keyPressed() {
- paused=!paused;
- }
- class myClass {
- int y;
- float x=random(0, width);
- void appear() {
- ellipse(x, y, 20, 20);
- y+=2;
- }
- }
1