Creating multiple instances of a class (fish) following different instances of a ball class
in
Programming Questions
•
1 year ago
okay so yeah. I've been picking code and hacking things together, but I've got this app that is under construction.
I've gotten one instance of this fish class to follow one instance of the ball class, but was curious as to how I could populate more fish following more balls.
i.e, fish 1 following ball 1, fish 2 following ball 2, etc.
Then again, I'm probably going about this in a really bulky way, and there's probably a better way to get the same behavior, but for now, I'd just like some help trying to get this to work.
so the code is as follows.
- Fish fish1;
- Ball ball;
- Rain rain;
- float ballx,bally; // location
- float[] xpos = new float[15];
- float[] ypos = new float[15];
- int[] rainXpos = new int[90];
- int[] rainYpos = new int[90];
- float circlex=150;
- float circley=150;
- //------------------SETUP-------------------
- void setup(){
- size(1024,768);
- background(136,216,247);
- smooth();
- //Call Families
- rain = new Rain();
- ball = new Ball(1);
- fish1 = new Fish();
- //Setup Rain
- for (int c = 0; c < rainXpos.length; c++)
- {
- rainXpos[c] = 0;
- rainYpos[c] = 0;
- }
- //Fish
- for (int i = 0; i < xpos.length; i++)
- {
- xpos[i]= 0 ;
- ypos[i]= 0 ;
- }
- }
- //-------------------DRAW--------------------
- void draw(){
- background (168,225,247);
- rain.draw();
- fish1.display(width/2, height/2);
- ball.move();
- ball.display();
- }
Ball class:
- class Ball {
- float r; // radius
- float speedx,speedy; // speed
- // Constructor
- Ball(float tempR) {
- r = tempR;
- ballx = random(width);
- bally = random(height);
- speedx = random( - 5,5);
- speedy = random( - 5,5);
- }
- void move() {
- ballx += speedx; // Increment x
- bally += speedy; // Increment y
- // Check horizontal edges
- if (ballx > width || ballx < 0) {
- speedx *= - 1;
- }
- //Check vertical edges
- if (bally > height || bally < 0) {
- speedy *= - 1;
- }
- }
- // Draw the ball
- void display() {
- fill(136,216,247);
- ellipse(ballx,bally,r*2,r*2);
- }
- }
- class Fish {
- float xspeed;
- float yspeed;
- float mov = 0;
- float mi = 0.1;
- void display(int x, int y){
- //Lilypad Following
- if (mousePressed == true) {
- mov = mov + mi ;
- if (mov > .4 || mov < -.4) {mi = mi * -1; }
- xspeed = (((mouseX-circlex))/120);
- yspeed = (((mouseY-circley))/120);
- circlex += xspeed + mov/2;
- circley += yspeed + mov;
- if (circlex > width) {circlex=width;}
- if (circley >height) {circley=height;}
- for (int i = 1; i<xpos.length-1; i++){
- xpos[i] = xpos[i+1];
- ypos[i] = ypos[i+1];
- }
- }
- //end mousepressed
- //Maxe this follow ballx, bally
- else {
- mov = mov + mi ;
- if (mov > .4 || mov < -.4) {mi = mi * -1; }
- xspeed = (((ballx-circlex))/150);
- yspeed = (((bally-circley))/150);
- circlex += xspeed + mov/2;
- circley += yspeed + mov;
- if (circlex > width) {circlex=width;}
- if (circley >height) {circley=height;}
- for (int i = 1; i<xpos.length-1; i++){
- xpos[i] = xpos[i+1];
- ypos[i] = ypos[i+1];
- }
- }
- xpos[xpos.length-1] = circlex;
- ypos[ypos.length-1] = circley;
- for (int i =0;i < xpos.length; i++)
- {
- noStroke();
- fill(2,51,59);
- ellipse(xpos[i],ypos[i],((xpos.length)/3.5)-i,((xpos.length)/2)-i);
- }
- }
- }
- class Rain {
- void draw()
- {
- fill(0);
- rainXpos[rainXpos.length-1] = int(random(1,width));
- rainYpos[rainYpos.length-1] = int(random(1,height));
- for (int c =0; c < rainXpos.length -1 ; c++)
- {
- rainXpos[c]=rainXpos[c+1];
- rainYpos[c]=rainYpos[c+1];
- }
- for (int c =0; c< rainXpos.length; c++)
- {
- stroke(255,255,255,c);
- noFill();
- ellipse(rainXpos[c],rainYpos[c],90-c,90-c);
- }
- }
- }
1