How to duplicate a shape when mouse is clicked

edited June 2017 in Questions about Code

Hi I'm new to processing. I'm just having another issue with my mini fishing game. I'm trying to get it so that when the fish is clicked on and restarts, it duplicates that fish each time. I've tried adding the shape again under the if statement in mousepressed(). But it's just doing the same thing. I've been trying to research online about it, but I can't find much on duplicating pshapes. So I don't know how to go about it. Thank you.

PShape fish1, body1, tail1, eye1, fish2, body2, tail2, eye2, fish3, body3, tail3, eye3, fish4, body4, tail4, eye4;
int xPos1 = 800;
int xPos2 = 800;
int xPos3 = 800;
int xPos4 = 800;

float per1 = sin(radians(10 * frameCount)); 
float per2 = sin(radians(20 * frameCount)); 
float per3 = sin(radians(30 * frameCount)); 
float per4 = sin(radians(frameCount));
float r = random(0, 600);
float yPos1 = random(0, 400);
float yPos2 = random(0, 400);
float yPos3 = random(0, 400);
float yPos4 = random(0, 400);

boolean qPressed = false;
boolean wPressed = false;
boolean ePressed = false;
boolean rPressed = false;

void setup() {
  size(800, 700);
  background(80, 208, 232);
  smooth();
  noStroke();

  //Creating the fish as a group of shapes parented together
  //fish 1
  fish1 = createShape(GROUP);
  tail1 = createShape (TRIANGLE, 22, 0, 60, -15 + per2, 60, 15 + per2); 
  tail1.setFill(color(252, 193, 97));
  body1 = createShape (ELLIPSE, 0, 0, 70, 50);
  body1.setFill(color(252, 193, 97));
  eye1 = createShape (ELLIPSE, -15, -5, 10, 10);
  eye1.setFill(color(0));

  fish1.addChild(tail1);
  fish1.addChild(body1);
  fish1.addChild(eye1);

  //fish 2    
  fish2 = createShape(GROUP);
  tail2 = createShape (TRIANGLE, 50, 0, 65, -30 + per2, 65, 30 + per2);
  tail2.setFill(color(160, 159, 158));
  body2 = createShape (ELLIPSE, 0, 0, 100, 35);
  body2.setFill(color(160, 159, 158));
  eye2 = createShape(ELLIPSE, -30, -5, 10, 10);
  eye2.setFill(color(0));

  fish2.addChild(tail2);
  fish2.addChild(body2);
  fish2.addChild(eye2);

  //fish 3
  fish3 = createShape(GROUP);
  tail3 = createShape (TRIANGLE, 20, 0, 50, -20 + per3, 50, 20 + per3);
  tail3.setFill(color(227, 156, 214));
  body3 = createShape (ELLIPSE, 0, 0, 60, 80);
  body3.setFill(color(227, 156, 214));
  eye3 = createShape (ELLIPSE, -20, -5, 10, 10);
  eye3.setFill(color(0));

  fish3.addChild(tail3);
  fish3.addChild(body3);
  fish3.addChild(eye3);

  //fish 4
  fish4 = createShape(GROUP);
  tail4 = createShape (TRIANGLE, 15, 0, 25, -10 + per3, 25,10 + per3);
  tail4.setFill(color(195, 247, 234));
  body4 = createShape (ELLIPSE, 0, 0, 40, 20);
  body4.setFill(color(195, 247, 234));
  eye4 = createShape (ELLIPSE, -10, 0, 5, 5);
  eye4.setFill(color(0));

  fish4.addChild(tail4);
  fish4.addChild(body4);
  fish4.addChild(eye4);

}

void draw() {

  //draw a looping fish swimming horizontally across the screen in random y coordinate each time it reaches the end.
  background(80, 208, 232);

  //fish 1 loop
  shape(fish1, xPos1, yPos1);
  xPos1 = xPos1-3;

  if (xPos1<-200) {
    xPos1 = 800;
    yPos1 = random(0, 400); 
  } 

  //fish 2 loop
  shape(fish2, xPos2, yPos2);
  xPos2 = xPos2-4;

  if (xPos2<-200) {
    xPos2 = 800;
    yPos2 = random(0, 400);
  }

  //fish 3 loop
  shape(fish3, xPos3, yPos3);
  xPos3 = xPos3-2;

  if (xPos3<-200) {
    xPos3 = 800;
    yPos2 = random(0, 400);
  }

  //fish 4 loop
  shape(fish4, xPos4, yPos4);
  xPos4 = xPos4 - 5;

  if (xPos4<-200) {
    xPos4 = 800;
    yPos4 = random(0, 400);
  }

  //Boxes that have the bait in them
  noFill();
  // Q
  if(qPressed == true) {
    stroke(203, 0, 0);
    strokeWeight(3);
  } else {
    stroke(1);
    strokeWeight(1);
  }
  rect(250, 620, 75, 75);

  // W
  if(wPressed == true) {
    stroke(203, 0, 0);
    strokeWeight(3);
  } else {
    stroke(1);
    strokeWeight(1);
  }
  rect(330, 620, 75, 75);

  // E
  if(ePressed == true) {
    stroke(203, 0, 0);
    strokeWeight(3);
  } else {
    stroke(1);
    strokeWeight(1);
  }
  rect(410, 620, 75, 75);

  // R
  if(rPressed == true) {
    stroke(203, 0, 0);
    strokeWeight(3);
  } else {
    stroke(1);
   strokeWeight(1);
  }
  rect(490, 620, 75, 75);

  }

void keyPressed() {

  //When 'q' or 'Q' key is pressed, the first box will turn red. When another key is pressed it will go back to black.
  if (key == 'q' || key == 'Q') {
    qPressed = true;
    wPressed = false;
    ePressed = false;
    rPressed = false;
  } 

  //When 'w' or 'W' key is pressed, the second box will turn red. When another key is pressed it will go back to black.
  else if (key == 'w' || key == 'W') {
    qPressed = false;
    wPressed = true;
    ePressed = false;
    rPressed = false;
  }

  //When 'e' or 'E' key is pressed, the third box will turn red. When another key is pressed it will go back to black.
  else if (key == 'e' || key == 'E') {
    qPressed = false;
    wPressed = false;
    ePressed = true;
    rPressed = false;
  } 

  //When 'r' or 'R' key is pressed, the fourth box will turn red. When another key is pressed it will go back to black.
  else if (key == 'r' || key == 'R') {
    qPressed = false;
    wPressed = false;
    ePressed = false;
    rPressed = true;
  }  
}

void mousePressed(){
  //When q is pressed (first box selected) and mouse is pressed on fish 1, that fish goes back to the beginning
 if (qPressed == true && dist(xPos1, yPos1, mouseX, mouseY) < 40){
  shape(fish1, xPos1, yPos1);
  xPos1 = 1000;
 }
 else if (qPressed == false && dist(xPos1, yPos1, mouseX, mouseY) < 40){
 shape(fish1, xPos1, yPos1);

 }

if (wPressed == true && dist(xPos2, yPos2, mouseX, mouseY) < 30){
  shape(fish2, xPos2, yPos2);
  xPos2 = 1200;
 }
 else if (wPressed == false && dist(xPos2, yPos2, mouseX, mouseY) < 30){
 shape(fish2, xPos2, yPos2);
 }

 if (ePressed == true && dist(xPos3, yPos3, mouseX, mouseY) < 40){
  shape(fish3, xPos3, yPos3);
  xPos3 = 1000;
 }
 else if (ePressed == false && dist(xPos3, yPos3, mouseX, mouseY) < 40){
 shape(fish3, xPos3, yPos3);
 }

if (rPressed == true && dist(xPos4, yPos4, mouseX, mouseY) < 20){
  shape(fish4, xPos4, yPos4);
  xPos4 = 1300;
 }
 else if (qPressed == false && dist(xPos4, yPos4, mouseX, mouseY) < 20){
 shape(fish4, xPos4, yPos4);
 }

}

Answers

  • look for array in the tutorials

  • I've done some research on arrays and I'm really struggling to wrap my noobie head around it. Here below is my attempt. Why does the custom fish shapes not work in the class? Sorry if I sound silly.

    PShape fish1, body1, tail1, eye1, fish2, body2, tail2, eye2, fish3, body3, tail3, eye3, fish4, body4, tail4, eye4;
    
    float per1 = sin(radians(10 * frameCount)); 
    float per2 = sin(radians(20 * frameCount)); 
    float per3 = sin(radians(30 * frameCount)); 
    float per4 = sin(radians(frameCount));
    float r = random(0, 600);
    
    boolean qPressed = false;
    boolean wPressed = false;
    boolean ePressed = false;
    boolean rPressed = false;
    
    FishC[] fish = new FishC[4];
    
    void setup() {
      size(800, 700);
      background(80, 208, 232);
      smooth();
      noStroke();
    
      fish[0] = fish1;
      fish[1] = fish2;
      fish[2] = fish3;
      fish[3] = fish4;
    
      //Creating the fish as a group of shapes parented together
      //fish 1
      fish1 = createShape(GROUP);
      tail1 = createShape (TRIANGLE, 22, 0, 60, -15 + per2, 60, 15 + per2); 
      tail1.setFill(color(252, 193, 97));
      body1 = createShape (ELLIPSE, 0, 0, 70, 50);
      body1.setFill(color(252, 193, 97));
      eye1 = createShape (ELLIPSE, -15, -5, 10, 10);
      eye1.setFill(color(0));
    
      fish1.addChild(tail1);
      fish1.addChild(body1);
      fish1.addChild(eye1);
    
      //fish 2    
      fish2 = createShape(GROUP);
      tail2 = createShape (TRIANGLE, 50, 0, 65, -30 + per2, 65, 30 + per2);
      tail2.setFill(color(160, 159, 158));
      body2 = createShape (ELLIPSE, 0, 0, 100, 35);
      body2.setFill(color(160, 159, 158));
      eye2 = createShape(ELLIPSE, -30, -5, 10, 10);
      eye2.setFill(color(0));
    
      fish2.addChild(tail2);
      fish2.addChild(body2);
      fish2.addChild(eye2);
    
      //fish 3
      fish3 = createShape(GROUP);
      tail3 = createShape (TRIANGLE, 20, 0, 50, -20 + per3, 50, 20 + per3);
      tail3.setFill(color(227, 156, 214));
      body3 = createShape (ELLIPSE, 0, 0, 60, 80);
      body3.setFill(color(227, 156, 214));
      eye3 = createShape (ELLIPSE, -20, -5, 10, 10);
      eye3.setFill(color(0));
    
      fish3.addChild(tail3);
      fish3.addChild(body3);
      fish3.addChild(eye3);
    
      //fish 4
      fish4 = createShape(GROUP);
      tail4 = createShape (TRIANGLE, 15, 0, 25, -10 + per3, 25,10 + per3);
      tail4.setFill(color(195, 247, 234));
      body4 = createShape (ELLIPSE, 0, 0, 40, 20);
      body4.setFill(color(195, 247, 234));
      eye4 = createShape (ELLIPSE, -10, 0, 5, 5);
      eye4.setFill(color(0));
    
      fish4.addChild(tail4);
      fish4.addChild(body4);
      fish4.addChild(eye4);
    
    }
    
    void draw() {
    
      //draw a looping fish swimming horizontally across the screen in random y coordinate each time it reaches the end.
      background(80, 208, 232);
    
      //Boxes that have the bait in them
      noFill();
      // Q
      if(qPressed == true) {
        stroke(203, 0, 0);
        strokeWeight(3);
      } else {
        stroke(1);
        strokeWeight(1);
      }
      rect(250, 620, 75, 75);
    
      // W
      if(wPressed == true) {
        stroke(203, 0, 0);
        strokeWeight(3);
      } else {
        stroke(1);
        strokeWeight(1);
      }
      rect(330, 620, 75, 75);
    
      // E
      if(ePressed == true) {
        stroke(203, 0, 0);
        strokeWeight(3);
      } else {
        stroke(1);
        strokeWeight(1);
      }
      rect(410, 620, 75, 75);
    
      // R
      if(rPressed == true) {
        stroke(203, 0, 0);
        strokeWeight(3);
      } else {
        stroke(1);
       strokeWeight(1);
      }
      rect(490, 620, 75, 75);
    
      }
    
      class FishC{
    
      int xPos1 = 800;
      int xPos2 = 800;
      int xPos3 = 800;
      int xPos4 = 800;
      float yPos1 = random(0, 400);
      float yPos2 = random(0, 400);
      float yPos3 = random(0, 400);
      float yPos4 = random(0, 400);
    
      //fish 1 loop
      shape(fish1, xPos1, yPos1);
      xPos1 = xPos1-3;
    
      if (xPos1<-200) {
        xPos1 = 800;
        yPos1 = random(0, 400); 
      } 
    
      //fish 2 loop
      shape(fish2, xPos2, yPos2);
      xPos2 = xPos2-4;
    
      if (xPos2<-200) {
        xPos2 = 800;
        yPos2 = random(0, 400);
      }
    
      //fish 3 loop
      shape(fish3, xPos3, yPos3);
      xPos3 = xPos3-2;
    
      if (xPos3<-200) {
        xPos3 = 800;
        yPos3 = random(0, 400);
      }
    
      //fish 4 loop
      shape(fish4, xPos4, yPos4);
      xPos4 = xPos4 - 5;
    
      if (xPos4<-200) {
        xPos4 = 800;
        yPos4 = random(0, 400);
      }
    
      }
    
    void keyPressed() {
    
      //When 'q' or 'Q' key is pressed, the first box will turn red. When another key is pressed it will go back to black.
      if (key == 'q' || key == 'Q') {
        qPressed = true;
        wPressed = false;
        ePressed = false;
        rPressed = false;
      } 
    
      //When 'w' or 'W' key is pressed, the second box will turn red. When another key is pressed it will go back to black.
      else if (key == 'w' || key == 'W') {
        qPressed = false;
        wPressed = true;
        ePressed = false;
        rPressed = false;
      }
    
      //When 'e' or 'E' key is pressed, the third box will turn red. When another key is pressed it will go back to black.
      else if (key == 'e' || key == 'E') {
        qPressed = false;
        wPressed = false;
        ePressed = true;
        rPressed = false;
      } 
    
      //When 'r' or 'R' key is pressed, the fourth box will turn red. When another key is pressed it will go back to black.
      else if (key == 'r' || key == 'R') {
        qPressed = false;
        wPressed = false;
        ePressed = false;
        rPressed = true;
      }  
    }
    
    void mousePressed(){
      //When q is pressed (first box selected) and mouse is pressed on fish 1, that fish goes back to the beginning
     if (qPressed == true && dist(xPos1, yPos1, mouseX, mouseY) < 40){
      shape(fish1, xPos1, yPos1);
      xPos1 = 1000;
     }
     else if (qPressed == false && dist(xPos1, yPos1, mouseX, mouseY) < 40){
     shape(fish1, xPos1, yPos1);
    
     }
    
    if (wPressed == true && dist(xPos2, yPos2, mouseX, mouseY) < 30){
      shape(fish2, xPos2, yPos2);
      xPos2 = 1200;
     }
     else if (wPressed == false && dist(xPos2, yPos2, mouseX, mouseY) < 30){
     shape(fish2, xPos2, yPos2);
     }
    
     if (ePressed == true && dist(xPos3, yPos3, mouseX, mouseY) < 40){
      shape(fish3, xPos3, yPos3);
      xPos3 = 1000;
     }
     else if (ePressed == false && dist(xPos3, yPos3, mouseX, mouseY) < 40){
     shape(fish3, xPos3, yPos3);
     }
    
    if (rPressed == true && dist(xPos4, yPos4, mouseX, mouseY) < 20){
      shape(fish4, xPos4, yPos4);
      xPos4 = 1300;
     }
     else if (qPressed == false && dist(xPos4, yPos4, mouseX, mouseY) < 20){
     shape(fish4, xPos4, yPos4);
     }
    
    }
    
  • You need to make the fish shape in the constructor of the class

    See tutorials | objects

  • Answer ✓

    This:

    PShape fish1, body1, tail1, eye1, fish2, body2, tail2, eye2, fish3, body3, tail3, eye3, fish4, body4, tail4, eye4;

    you have to get rid of

    body, tail eye should be part of the class and since you have an array of that class now, you need only one tail and eye etc. inside the class

  • Alright updated code. I've got it working with some help from a friend. Only small problem I have left is how do I get rid of the stroke on the duplicate fish?

    // Variables
    float r = random(0, 600);
    float per1 = sin(radians(10 * frameCount)); 
    float per2 = sin(radians(20 * frameCount)); 
    float per3 = sin(radians(30 * frameCount)); 
    float per4 = sin(radians(frameCount));
    boolean qPressed = false;
    boolean wPressed = false;
    boolean ePressed = false;
    boolean rPressed = false;
    // Stores all fish
    ArrayList<Fish> fish = new ArrayList<Fish>();
    void setup() {
        size(800, 700);
        background(80, 208, 232);
        smooth();
        noStroke();
        // Add Fish - Do this for each fish
        // Fish(per, color1, color2, xPos, yPos, speed, type, then a bunch of values that were used in setting up your shape - IDK what they do);
        fish.add(new Fish(per2, color(252, 193, 97), color(0), 800, random(0, 400), 3, 1, 40, 22, 0, 60, -15, 60, 15, 0, 0, 70, 50, -15, -5, 10, 10));
    }
    void draw() {
        background(80, 208, 232);
    
        // Update each fish
        for (Fish f : fish) {
            MoveFish(f);
        }
    
        // (I'd also turn the boxes with bait into a method too)
        //Boxes that have the bait in them
    
        // Q
        if(qPressed == true) {
            stroke(203, 0, 0);
            strokeWeight(3);
        } else {
            stroke(1);
            strokeWeight(1);
        }
        rect(250, 620, 75, 75);
        // W
        if(wPressed == true) {
            stroke(203, 0, 0);
            strokeWeight(3);
        } else {
            stroke(1);
            strokeWeight(1);
        }
        rect(330, 620, 75, 75);
        // E
        if(ePressed == true) {
            stroke(203, 0, 0);
            strokeWeight(3);
        } else {
            stroke(1);
            strokeWeight(1);
        }
        rect(410, 620, 75, 75);
        // R
        if(rPressed == true) {
            stroke(203, 0, 0);
            strokeWeight(3);
        } else {
            stroke(1);
            strokeWeight(1);
        }
        rect(490, 620, 75, 75);
    }
    void MoveFish(Fish fish) {
        shape(fish.fish, fish.xPos, fish.yPos);
        fish.xPos = fish.xPos - fish.speed;
        if (fish.xPos < -200) {
            fish.xPos = 800;
            fish.yPos = random(0, 400);
        }
    }
    void keyPressed() {
      //When 'q' or 'Q' key is pressed, the first box will turn red. When another key is pressed it will go back to black.
      if (key == 'q' || key == 'Q') {
        qPressed = true;
        wPressed = false;
        ePressed = false;
        rPressed = false;
      } 
      //When 'w' or 'W' key is pressed, the second box will turn red. When another key is pressed it will go back to black.
      else if (key == 'w' || key == 'W') {
        qPressed = false;
        wPressed = true;
        ePressed = false;
        rPressed = false;
      }
    
      //When 'e' or 'E' key is pressed, the third box will turn red. When another key is pressed it will go back to black.
      else if (key == 'e' || key == 'E') {
        qPressed = false;
        wPressed = false;
        ePressed = true;
        rPressed = false;
      } 
    
      //When 'r' or 'R' key is pressed, the fourth box will turn red. When another key is pressed it will go back to black.
      else if (key == 'r' || key == 'R') {
        qPressed = false;
        wPressed = false;
        ePressed = false;
        rPressed = true;
      }  
    }
    void mousePressed() {
        // Loop through fish
        for (Fish f : fish) {
            // First check distance
            if (dist(f.xPos, f.yPos, mouseX, mouseY) < f.dist) {
                // Now check type
                if (f.type == 1) {
                    // Now check button
                    if (qPressed == true) {
                        shape(f.fish, f.xPos, f.yPos);
                        f.xPos = 1000;
                        Fish nF = f.DuplicateFish();
                        nF.yPos = random(0, 400);
                        fish.add(nF);
                        break;
                    } else {
                        shape(f.fish, f.xPos, f.yPos);
                    }
    
                } else if (f.type == 2) {
                    // Now check button
                    if (wPressed == true) {
                        shape(f.fish, f.xPos, f.yPos);
                        f.xPos = 1200;
                        Fish nF = f.DuplicateFish();
                        nF.yPos = random(0, 400);
                        fish.add(nF);
                        break;
                    } else {
                        shape(f.fish, f.xPos, f.yPos);
                    }
    
                } else if (f.type == 3) {
                    // Now check button
                    if (ePressed == true) {
                        shape(f.fish, f.xPos, f.yPos);
                        f.xPos = 1000;
                        Fish nF = f.DuplicateFish();
                        nF.yPos = random(0, 400);
                        fish.add(nF);
                        break;
                    } else {
                        shape(f.fish, f.xPos, f.yPos);
                    }
    
                } else if (f.type == 4) {
                    // Now check button
                    if (rPressed == true) {
                        shape(f.fish, f.xPos, f.yPos);
                        f.xPos = 1300;
                        Fish nF = f.DuplicateFish();
                        nF.yPos = random(0, 400);
                        fish.add(nF);
                        break;
                    } else {
                        shape(f.fish, f.xPos, f.yPos);
                    }
                }
            }
        }
    }
    // Class for a fish
    // Basically stores it all together in one clean class so that we can create an array of them
    class Fish {
        PShape fish, body, tail, eye;
        float xPos;
        float yPos;
        int speed; // how fast the fish moves
        int type; // type of fish, 1/2/3/4
        int dist; // how much clicking distance
        float per;
        color col;
        color col2;
        int a1, a2, a3, a4, a5, a6, b1, b2, b3, b4, c1, c2, c3, c4, str;
        // Constructor for each fish
        // Fish(per, color1, color2, xPos, yPos, speed, type, then a bunch of values that were used in setting up your shape - IDK what they do);
        Fish(float p, color color1, color color2,float x, float y, int s, int t, int d, int a1, int a2, int a3, int a4, int a5, int a6, int b1, int b2, int b3, int b4, int c1, int c2, int c3, int c4) {
            this.per = p;
            this.col = color1;
            this.col2 = color2;
            this.xPos = x;
            this.yPos = y;
            this.speed = s;
            this.type = t;
            this.dist = d;
            this.a1 = a1;
            this.a2 = a2;
            this.a3 = a3;
            this.a4 = a4;
            this.a5 = a5;
            this.a6 = a6;
            this.b1 = b1;
            this.b2 = b2;
            this.b3 = b3;
            this.b4 = b4;
            this.c1 = c1;
            this.c2 = c2;
            this.c3 = c3;
            this.c4 = c4;
    
    
            this.fish = createShape(GROUP);
    
            this.tail = createShape(TRIANGLE, a1, a2, a3, a4 + per, a5, a6 + per);
            this.tail.setFill(col);
    
            this.body = createShape(ELLIPSE, b1, b2, b3, b4);
            this.body.setFill(col);
    
            this.eye = createShape(ELLIPSE, c1, c2, c3, c4);
            this.eye.setFill(col2);
    
            this.fish.addChild(tail);
            this.fish.addChild(body);
            this.fish.addChild(eye);
        }
    
        // Duplicates a fish that has the exact same values
        Fish DuplicateFish() {
            return new Fish(this.per, this.col, this.col2, this.xPos, this.yPos, this.speed, this.type, this.dist, this.a1, this.a2, this.a3, this.a4, this.a5, this.a6, this.b1, this.b2, this.b3, this.b4, this.c1, this.c2, this.c3, this.c4);
        }
    }
    

    Also thank you very much for the help!

  • The function MoveFish belongs into the class

    You don't have to pass a parameter to it then

    In line 27 just say f.MoveFish();

    Make some changes in the function MoveFish

  • edited June 2017

    As for the line you mentioned

    I am not at home so I can't test it - sorry!

    Can you find out where the line comes from?? What is causing the line?

    Suggestions:

    You could swap lines 222 and 223

    You could use noStroke in line 213 /216: // wrong

    Eg. tail.noStroke(); // wrong, see below

    Also you use a lot of shape() in mousePressed which I don't think you need // correct

    Chrisir

    [EDITED]

  • edited June 2017

    I tested a few things

    The Lines

    this let's draw you all fishs without the black line (the outline)

    use setStroke(false); for shapes (and not noStroke()) :

        tail = createShape(TRIANGLE, a1, a2, a3, a4 + per, a5, a6 + per);
        tail.setStroke(false); 
        tail.setFill(col);
    
        body = createShape(ELLIPSE, b1, b2, b3, b4);
        body.setStroke(false);
        body.setFill(col);
    
        eye = createShape(ELLIPSE, c1, c2, c3, c4);
        eye.setStroke(false);
        eye.setFill(col2);
    
        fish = createShape(GROUP);
        fish.setStroke(false);
        fish.addChild(body);
        fish.addChild(tail);   
        fish.addChild(eye);
    

    Parameters:

    Also, when you pass numbers to the class' constructor, that are the same for every fish, always, just store them into the class instead of passing them.

    I mean those:

        int a1, int a2, int a3, int a4, int a5, int a6, 
        int b1, int b2, int b3, int b4, 
        int c1, int c2, int c3, int c4
    

    More variety please

    Also, you don't have much variety in your fish

    consider

      // Duplicates a fish that has the exact same values
      Fish DuplicateFish() {
    
        float speedN = random(0.9, 3.9); // speed is of the variable type float now!!!!!
                         // change this in the class as well please
    
        int typeN =int(random(5))+1;
        typeN = constrain(type, 1, 4); 
    
        color colN = color(random(111, 255), random(111, 255), random(111, 255));
    
        return new Fish(per, colN, col2, xPos, yPos, speedN, typeN, dist, 
          a1, a2, a3, a4, a5, a6, b1, b2, b3, b4, c1, c2, c3, c4);
      }//method
    

    you can then shorten the lines 122 to 162 above when you use DuplicateFish()

    The per1, per2... Variables

    Let's consider the per1, per2... Variables.

    in setup:

    frameCount doesn't make sense here, it's probably always 0 in setup

    So I wrote this:

      float r = random(0, 600);
    
      float per1 = sin(radians(10 * r)); 
      float per2 = 20*sin(radians(2 * r)); 
      float per3 = sin(radians(30 * r)); 
      float per4 = sin(radians(r));
    

    only per2 is in use by the way.

    So consider this:

          float getPer() {
              float r = random(0, 600);
    
              float[] per = new float [5];   
    
              per[1] = 12*sin(radians(10 * r)); 
              per[2] = 20*sin(radians(2 * r)); 
              per[3] = 15*sin(radians(30 * r)); 
              per[4] = 16*sin(radians(r));
    
              int perType =int(random(5))+1;
              perType = constrain(perType, 1, 4);
    
              return per[perType];
        }
    

    your setup is then (note that per is now getPer() and we have a random eye color):

      // Add Fish - Do this for each fish
      float speed = random(0.9, 3.9);
    
      int type =int(random(5))+1;
      type = constrain(type, 1, 4);
    
      //           Fish(per,      color1,              color2,                  xPos,     yPos,  
      fish.add(new Fish(getPer(), color(252, 193, 97), color(random(111, 222)), width+33, random(0, 400), 
        //speed, type, 
        speed, type, 
        //then a bunch of values that were used in setting up your shape - IDK what they do);
        40, 22, 0, 60, -15, 60, 15, 0, 0, 70, 50, -15, -5, 10, 10));
    

    (you can also use this getPer() in DuplicateFish() )

    I leave it here.

    Have fun, come back for more questions.

    Best, Chrisir ;-)

  • if (rPressed == true) {

    same as

    if (rPressed) {

  • edited June 2017

    str is not in use in the class

  • That was such a big help thank you! And yer the only reason why there was only one fish type before was because I just wanted to get the first fish right before I added the rest. I've now added the other 3 types of fish to go with the bait. Also the per was only there originally to make the tails move up and down. That's back when I first made the fish and had them in draw. But when I needed to move them to setup they stopped working and I didn't bother to change them cause it wasn't high priority haha. I've fixed that all up now thanks to your help :)

  • edited June 2017

    Great!

  • Do you want me to show my code of it?

Sign In or Register to comment.