Home > database >  My code keeps getting an error that says duplicate method setup
My code keeps getting an error that says duplicate method setup

Time:10-13

I'm making a creature and need to add a moving background into it so I chose for bubbles. On it's own it works, same as the movement of the Pig. But when I combine them together it gives the error 'duplicate method setup() in my file. Does anyone have any tips what to do?

Piggy myPiggy;

void setup() {
  size(400, 400) ;
  background(255) ;
  myPiggy = new Piggy();
}
void draw() {
  background(255) ;
  myPiggy.display();

}
void mouseDragged() {
  myPiggy.mouseSmoved(mouseX, mouseY);

}

void setup() {
Bubble[] bubbles = new Bubble[300];

  size(640, 360);
  for (int i = 0; i < bubbles.length; i  ) {
    bubbles[i] = new Bubble(random(60));
  }
}

void draw() {
  background(255);
  for (int i = 0; i < bubbles.length; i  ) {
    bubbles[i].ascend();
    bubbles[i].display();
    bubbles[i].top();
  }
}
 
class Piggy {
  float pgX= 0 ;
  float pgY= 0 ;
  int stX = 0 ;
  int stY = 0 ;
  color earColor;
  color faceColor;
  
  Piggy() {
    stX = width / 2;
    stY = height / 2;
  }

  void display () {
    //ears
    fill(255,192,203) ;
    noStroke() ;
    ellipse(stX-90, stY-70, 60, 100) ;
    ellipse(stX 90, stY-70, 60, 100) ;
    fill(stX-90, stY-70, 40, 80) ;
    fill(stX 90, stY-70, 40, 80) ;

    // face
    fill(255,192,203) ;
    ellipse(stX, stY, 250, 220);

    // eyes
    fill(255) ;
    circle(stX-60, stY-30, 40) ;
    circle(stX 60, stY-30, 40) ;
    // pupil
    fill(0) ;
    circle(stX-60, stY-25, 15);
    circle(stX 60, stY-25, 15);

    // nose
    fill(253, 89, 157) ;
    ellipse(stX, stY 20, 60, 50) ;
    fill(253, 144, 214) ;
    noStroke() ;
    circle(stX-15, stY 20, 20) ;
    circle(stX 15, stY 20, 20) ;

    //mouth
    fill(255) ;
    noStroke() ;
    arc(stX, stY 60, 50, 40, 0, PI) ;
  }

  void mouseSmoved(int x, int y) {
    stX = x;
    stY = y;
    
  }
}


class Bubble {

  float x;
  float y;
  float diameter;
  float yspeed;

  Bubble(float tempD) {
    x = random(width);
    y = height;
    diameter = tempD;
    yspeed = random(0.5, 2.5);
  }

  void ascend() {
    y = y - yspeed;
    x = x   random(-2,2);
  }

  void display() {
    stroke(0);
    noFill();
    //fill(127,100);
    ellipse(x, y, diameter, diameter);
  }

  void top() {
    if (y < -diameter/2) {
      y = height diameter/2;
    }
  }
}

All help is appreciated as this is my first time coding and the errors are getting ahead of me haha.

CodePudding user response:

You are declaring setup twice. There are multiple solutions to this:
1: Remove one of the setups.
2: Overload the method. Overloading a method allows you to have multiple methods with the same name, but requires them to have a different amount of parameters. To do this, simply give one of the setups a parameter.
3: Rename one of the setups.

CodePudding user response:

Looks like you are using Processing. In that case as was pointed out the main file in Processing can have only one setup() method. A simple solution is to merge their method bodies into a single setup and remove the second one, like so

void setup() {
  size(400, 400) ;
  background(255) ;
  myPiggy = new Piggy();

  Bubble[] bubbles = new Bubble[300];

  // You cannot have two size() calls, pick one above or this one
  //size(640, 360);

  for (int i = 0; i < bubbles.length; i  ) {
    bubbles[i] = new Bubble(random(60));
  }
}

Please note that does not cover other aspects of your code you are trying to merge. For example, you'd need to mergedraw() to display your Piggy as well as the bubbles.

  • Related