Help resetting variables inside classes, arrays

Hi there! Full disclosure, this is for my beginner’s coding class, so I might not get the names of different resources correct or might have stuff in weird places - any advice is appreciated, but the simpler the better right now!

I’ve completed my code to the best of my ability, and the last thing I’m missing is my reset() function. When I press ‘r’, I’d like the movable boxes to go back to their original coordinates, the mask layer to clear, and the snow animation to reset - since many of my variables are within classes or arrays, I’m not quite sure what I’d need to add or change in my reset() to make it, you know, reset, or if there’s some simple code I could add to my classes to reset without needing the function.

I feel like part of it is where my variables are located, but it all makes sense to me as is at the moment, and I’m not sure how I’d have to rearrange it to fit.
Thank you so much for the help!


// Snow class modified from Ziv Schnieder : https://openprocessing.org/sketch/115994/

Snow[] flakes = new Snow[300];
SoundFile music;
SoundFile purr;

PGraphics msk;
PImage img;

void setup() {
  size (1280, 720);
  // background(0);
  for (int i = 0; i<flakes.length; i++) {
    flakes[i] = new Snow(random(2, 10));
    flakes[i].spreadY(i);
  }

  s = new Sqr[7]; // initialize array
  s[0] = new Sqr(100, 600, 20);
  s[1] = new Sqr(450, 650, 20);
  s[2] = new Sqr(600, 500, 20);
  s[3] = new Sqr(1120, 80, 20);
  s[4] = new Sqr(1060, 180, 20);
  s[5] = new Sqr(1180, 280, 20);
  s[6] = new Sqr(1110, 280, 20);

  music = new SoundFile (this, "White X mas.wav");
  purr = new SoundFile (this, "purr.wav");

  msk = createGraphics(1280, 720);
  img = loadImage("grizzly-look-down-peter-mangolds.jpg");
  img.resize(width, height);

  msk.beginDraw();
  msk.background(0, 0, 128);       
  msk.endDraw();

}

void draw() {
  background(255, 0, 0);

  // BACKGROUND GRAPHIC ----------------------------

  // SNOWFLAKES ------------------------------------
  for (int i = 0; i < flakes.length; i++) {
    flakes[i] .display();
  }

  // WINDOW "GLASS" --------------------------------

  msk.beginDraw();
  msk.fill(0, 0, 255);
  msk.noStroke();
  msk.circle(mouseX, mouseY, 20);
  msk.endDraw();

  // ERASER ----------------------------------------
  PImage img = g.copy();
  img.mask(msk);
  background(128);
  g.clear();
  image(img, 0, 0);

  // WINDOW FRAME ----------------------------------
  push();
  translate(500, 20);
  scale(.7);
  fill(0, 0, 255);
  rect(0, 0, 50, 720);
  rect(590, 0, 50, 720);
  rect(0, 0, 640, 50);
  rect(0, 670, 640, 50);
  rect(300, 0, 20, 720);
  rect(0, 340, 640, 20);
  pop();

  // MISC. PROPS ----------------------------------

  wallpaper();
  bedframe(); // includes cat asset
  bedside();
  shelf();

  // PETTING CAT --------------------------------------

  if ((mousePressed) && (mouseButton == RIGHT)) { // press right mouse button over hand to pet the cat
    push();
    fill(0);
    circle(430, constrain(mouseY, 300, 400), 10);
    pop();
  } else // hand asset stays for reference
  {
    push();
    fill(0);
    circle(430, 400, 10);
    pop();
  }

  // CHRISTMAS LIGHTS -----------------------------------------
  if (keyPressed) // press 'L' to turn on the string lights
  {
    if (key == 'l' || key == 'L')
    {
      stringLightsOn();
    }
  } else
  {
    stringLightsOff();
  }


  for (int i = 0; i < 7; i++) {
    s[i].display();
  }

  reset();
}

void mousePressed() {

  // PILLOWS AND STUFFED ANIMALS -----------------------------
  for (int i = 0; i < 7; i++) { // click on any of the images
    if ((mouseX >= s[i].x) && (mouseX <= s[i].x + s[i].wh)&&(mouseY >= s[i].y) && (mouseY <= s[i].y + s[i].wh))
    {

      s[i].selected = true;
      xOffset = mouseX-s[i].x;
      yOffset = mouseY-s[i].y;
    } else
    {
      s[i].selected = false;
    }
}
}

void mouseDragged() {
  for (int i = 0; i < 7; i++) { // drag the image wherever you'd like it to go
    if (s[i].selected == true)
    {
      stroke(255);
      s[i].x = mouseX -xOffset;
      s[i].y = mouseY -yOffset;
    }
  }
}

void mouseClicked()
{
  if ((mouseX >= 870-75) && (mouseX <= 870+75)&&(mouseY >= 570-50) && (mouseY <= 570+50)) // click on the radio to play music
  {
    music.play();
  }

  if ((mouseButton == LEFT) && (mouseX >= 430-100) && (mouseX <= 430+100)&&(mouseY >= 400-100) && (mouseY <= 400+100)) // left-click on the cat to hear him purr
  {
    purr.play();
  }
}

// CLASSES, FUNCTIONS, ETC. ----------------------------------------------------

class Snow {
  float x;
  float y;
  float alpha;
  float diameter;
  float speed = random(.1, .9);
  float descentX;

  Snow (float tempD) {
    x = random(-50, width+50);
    y = random(0, 40);
    diameter = tempD;
  }

  void spreadY(int i) {
    y = y - i*3;
  }

  void display() {
    alpha = map(y, 0, height, 255, -50);
    noStroke();
    fill(255, alpha);
    ellipse(x, y, diameter, diameter);
    y = y + speed;
    x = x + descentX;

    //checking the boundary
    if (y > height) {
      y = -diameter;
    }
    if (x < 0-50) {
      x = width+diameter;
    } else if (x > width+50) {
      x = 0-diameter;
    }
  }
}

Sqr[] s;

float xOffset = 0.0;
float yOffset = 0.0;

class Sqr {
  float x, y, wh;
  boolean selected;

  Sqr(float xpos, float ypos, float wihi) {
    x = xpos;
    y = ypos;
    wh = wihi;
  }

  void display() {
    noStroke();
    square(x, y, wh);
  }
  
}

void wallpaper()
{
  fill(0, 0, 150);
  rect(0, 0, 500, 720);
  rect(500, 0, 1280, 20);
  rect(945, 0, 600, 720);
  rect(500, 525, 500, 500);
}

void bedframe()
{
  fill(255, 100, 0);
  rect(0, 450, 700, 500);
  fill(200);
  circle(430, 400, 100);
}

void bedside()
{
  push();
  fill(255, 100, 100);
  rect(730, 600, 250, 200);
  fill(0);
  rect(870, 570, 75, 50);
  pop();
}

void shelf()
{
  push();
  fill(255, 100, 100);
  rect(1000, 100, 200, 10);
  rect(1000, 200, 200, 10);
  rect(1000, 300, 200, 10);
  pop();
}
void stringLightsOff()
{
  push();
  fill(0);
  circle(100, 100, 30);
  pop();
}
void stringLightsOn()
{
  push();
  fill(255);
  circle(100, 100, 50);
  pop();
}

void reset()
{
  if (keyPressed == true)
  {
    if (key == 'r' || key == 'R')
    {
      //reset Sqr to original positions
      // reset snow
      music.stop();
      purr.stop();
      // reset mask drawings
    }
  }
}

Hello @moffmix,

This will restart from setup():

void keyPressed()
  {
  if (key == 'r' || key == 'R')
    {
    music.stop();
    purr.stop();
    frameCount = -1;  //next frame is setup() which is frameCount = 0
    }
  }

References:

:)

Wow, thank you so much ! If I’ve learned anything from this class, it’s that I’m really good at oversimplifying what ends up as a single line of code :roll_eyes:. I really appreciate it!

1 Like

Call this again for reset

1 Like