I am trying to make the fish move....

PImage fishImage, booty;
int q=1;
int p=1;

void setup() {
  size(600, 600);
  background(#124FB4);
}
void draw() {
  ArrayList<Fish> fish=new ArrayList();
  fishImage=loadImage("fish.jpg"); //load the image
  booty=loadImage("booty.jpg");
  if (q==1) {
    for (int i=0; i<6; i++) {  // yes i know it would have been simpler to put this in the setup() method but i do not like simple... :)
      int x=int(random(width/5, width-50)); //x of fish
      int y=int(random(height/5, height-50)); //y of fish
      fish.add(new Fish(x, y));
      println(i);
      fill(#1203FF);
      stroke(#15D3D2);
      ellipse(x-15, y-5, 5, 5);
      ellipse(x-20, y-20, 5, 5);
      ellipse(x+5, y-45, 5, 5);
      q=2;
    }
    image(booty, int(random(50, width-50)), height-75, 50, 50);
  }
  if (p==1) {
    for (int i=0; i<1000; i++) { //draw rocks as ellipses
      int x=int(random(width));
      int y=int(random(height-25, height));
      noStroke();
      int w=int(random(5));


      //if w==0 then it will fill the color of bubbles
      if (w==1)
        fill(#37EA1C);
      if (w==2)
        fill(#FFEB08);
      if (w==3)
        fill(#08F9FF);
      if (w==4)
        fill(#FF0808);
      if (w==5)
        fill(#FF08B9);

      ellipse(x, y, 10, 10);
      p=2;
    }
  }
  for (int i=0; i<fish.size (); i++) {
    fish.get(i).display();
  }
  for (int i=0; i<fish.size (); i++) {
    fish.get(i).move();
  }
}

class Fish {
  private int x, y;
  public Fish(int xpos, int ypos) {
    x=xpos;
    y=ypos;
  }
  void display() {
    image(fishImage, x, y, width/15, height/15);
  }
  void move() { //this is the stuff that is not executing correctly. i want it to constantly increase x but it is not increasing x at all....
    x+=5;
    if (x>width) {
      x=0;
      println(x);
    }
  }
}

Answers

  • No.

    NO.

    NO!!! NO!!!!! NO!!!!!!!

    NOOOOOOOOOOOOOOOOOOOOOOOOO!!!!!!!!!!!!!!

    X(

    DO NOT USE loadImage() IN draw()!

  • @TfGuy44 what is the method i should be using instead of loadImage()?

  • @TfGuy44 could you please tell me how to make the fish move in the move method under the fish class? mine is not working

  • Please put the lines that use loadImage() into setup() before you do anything else.

  • ok and how do i make the fish move?

  • PImage fishImage, booty, seaAn, coral;
    int q=1;
    int p=1;
    
    void setup() {
      size(600, 600);
      fishImage=loadImage("fish.jpg"); //load the image
      booty=loadImage("booty.jpg");
      seaAn=loadImage("sea anemone.jpg");
      coral=loadImage("coral.jpg");
    }
    void draw() {
      ArrayList<Fish> fish=new ArrayList();
      background(#124FB4);
      if (q==1) {
        for (int i=0; i<6; i++) {  // yes i know it would have been simpler to put this in the setup() method but i do not like simple... :)
          int x=int(random(width/5, width-50)); //x of fish
          int y=int(random(height/5, height-50)); //y of fish
          fish.add(new Fish(x, y));
          println(i);
          fill(#1203FF);
          stroke(#15D3D2);
          ellipse(x-15, y-5, 5, 5);
          ellipse(x-20, y-20, 5, 5);
          ellipse(x+5, y-45, 5, 5);
          q=2;
        }
        int t=width-150;
        t-=20;
        println(t);
        image(booty, int(random(width-50)), height-75, 50, 50);
        image(seaAn, int(random(t)), height-160, 150, 150);
        image(coral, int(random( width-400)), height-500, 300, 300);
      }
      if (p==1) {
        for (int i=0; i<1000; i++) { //draw rocks as ellipses
          int x=int(random(width));
          int y=int(random(height-25, height));
          noStroke();
          int w=int(random(5));
    
    
          //if w==0 then it will fill the color of bubbles
          if (w==1)
            fill(#37EA1C);
          if (w==2)
            fill(#FFEB08);
          if (w==3)
            fill(#08F9FF);
          if (w==4)
            fill(#FF0808);
          if (w==5)
            fill(#FF08B9);
    
          ellipse(x, y, 10, 10);
          p=2;
        }
      }
      for (int i=0; i<fish.size (); i++) {
        fish.get(i).display();
      }
      for (int i=0; i<fish.size (); i++) {
        fish.get(i).move();
      }
    }
    
    class Fish {
      private int x, y;
      public Fish(int xpos, int ypos) {
        x=xpos;
        y=ypos;
      }
      void display() {
        image(fishImage, x, y, width/15, height/15);
      }
      void move() {
        x+=5;
        if (x>width) {
          x=0;
          println(x);
        }
      }
    }
    

    here is my updated code.... how can i make the fish move? that is the only part that i need help with... Thanks @TfGuy44

  • edited June 2015 Answer ✓

    The problem is that your ArrayList of Fish, fish, is defined inside draw(), and so it is re-created every frame. But fish are only added to it on the first frame, when q==1!

    Move line 13 to the top.

  • edited June 2015

    @TfGuy44 can you email with me to get the file?

  • @TfGuy44 I sent you the email containing the code and the data

  • Answer ✓
    PImage fishImage;
    ArrayList<Fish> fish=new ArrayList();
    
    void setup() {
      size(600, 600);
      fishImage=loadImage("fish.jpg"); //load the image
      for (int i=0; i<6; i++) {
        int x=int(random(width/5, width-50)); //x of fish
        int y=int(random(height/5, height-50)); //y of fish
        fish.add(new Fish(x, y));
      }
    }
    void draw() {
      background(255);
      for (int i=0; i<fish.size (); i++) {
        fish.get(i).display();
        fish.get(i).move();
      }
    }
    
    class Fish {
      private int x, y;
      public Fish(int xpos, int ypos) {
        x=xpos;
        y=ypos;
      }
      void display() {
        image(fishImage, x, y, width/15, height/15);
      }
      void move() {
        x-=5;
        if (x<-fishImage.width) {
          x=width;
          println(x);
        }
      }
    }
    

    I cut it down to just the fish. If you want to draw additional things, define additional global variables to remember them, create them in setup(), and update and render them in draw(). You should NOT rely on what was drawn in the previous frame - redraw every element that should be visible every time draw() runs.

  • edited June 2015

    @TfGuy44 how would I reverse them if they are at or greater than width? I currently have

        if (x>=width) {
          println(x>=width);
          speedX=speedX*-1;
        }
    
  • The line x-=5 updates the Fish's position. If you give it a speed, you would have to use that new speed variable to update the position, instead of the constant 5.

  • where do I put the speedX variable here?

    void move() {
        float speedX;
        float speedY;
        if (x<=0) {
          speedX=.5;
          x+=speedX; //increases x by speedx
          //y+=speedY;
        } else if (x>=width) {
          speedX=-.5;
          println(speedX);
          x+=speedX; //increases x by speedx
          //y+=speedY;
        } else if(speedX==-.5&&x>0){
          speedX=.5;
          x+=speedX;
          //y+=speedY;
        }
      }
    

    where it currently is the last else if statement cannot be executed because speedX has not been initialized... so where do i put speedX without it constantly updating speedX?

  • Define speed as a class variable, like x is, not as a local variable in your move() function.

    When your fish is off the left edge, set the speed to 1.

    When your fish is off the right edge, set the speed to -1.

    Always update the fish's position based on its speed.

  • Answer ✓

    it's a shame...

    all this is covered in the most basic examples

    he must have a very bad teacher....

  • Answer ✓

    the speed thing for the fish:

    • it's the same what we did with our active square, bouncing left and right in Stacker
  • edited June 2015

    @Chrisir yes, my teacher is very bad...

  • edited June 2015

    @Chrisir my updated code 100% working

     PImage fishImage, booty, seaAn, coral;
    int q=1;
    int p=1;
    ArrayList<Fish> fish=new ArrayList();
    ArrayList<Rocks> rock=new ArrayList(0);
    //6 fish in an arrayList
    //rocks in an arraylist
    //fish move left and right and vice versa
    //there are 5 objects besides fish in my aquarium
    //no objects other tahn fish move
    //fish turn when they hit a wall
    //fish descend and ascend
    //fish ascend and descend as they move left and right
    //fish randomly turn direction
    //i have comments indentation and good code structure. My variable names are also understandable
    
    
    void setup() {
      size(600, 600);
    
      for (int i=0; i<6; i++) {  
        int x=int(random(width/5, width-50)); //x of fish
        int y=int(random(height/5, height-50)); //y of fish
        fish.add(new Fish(x, y)); //my array list of fish x and y positions
      }
      println(fish.size());
      fishImage=loadImage("fish.gif"); //load the image
      booty=loadImage("booty.gif");
      seaAn=loadImage("sea anemone.jpg");
      coral=loadImage("coral.jpg");
      for (int i=0; i<1000; i++) { //draw rocks as ellipses
        int xRock=int(random(width));
        int yRock=int(random(height-25, height));
        rock.add(new Rocks(xRock, yRock));
        //ellipse(x, y, 10, 10);
      }
    }
    void draw() {
      Booty bootyy=new Booty(530, height-85);
      background(#124FB4);
      image(seaAn, 330, height-160, 150, 150); //sea anemone 
      image(coral, 394, height-500, 300, 300); //coral
      //image(booty, 530, height-75, 50, 50);
      bootyy.display();
      for (int i=0; i<fish.size (); i++) {
        fish.get(i).display(); //displays fish
        fish.get(i).moveX(); //initializes x stuff
        fish.get(i).moveY(); //initializes y stuff
      }
      //if (p==1) {
    
      for (int i=0; i<rock.size (); i++)
        rock.get(i).display(); //draws ellipses at bottom of tank
      //p-=1;
      //}
    }
    
    class Fish {
      private float x, y;
      private float speedX=.5;
      private float speedY=.5; 
      int requestx=int(random(50,width-50)); //requests new xposition
      int requesty=int(random(50,height-100)); //requests new y position 
      public Fish(float xpos, float ypos) {
        x=xpos;
        y=ypos;
      }
      void display() { //displays fish
        fill(#1203FF);
        stroke(#15D3D2);
        ellipse(x-15, y-5, 5, 5);
        ellipse(x-20, y-20, 5, 5);
        ellipse(x+5, y-45, 5, 5);
        image(fishImage, x, y, width/15, height/15);
      }
      void moveX() {  //moves the x pos of fish
        if (x==requestx)
          requestx=int(random(width));
        if (x<requestx) {
          x+=speedX;
        } else if (x>=requestx) {
          x-=speedX;
        } else if (x==requestx) {
          requestx=int(random(width));
        }
      }
      void moveY() { //moves y pos of fish
        if (y==requesty)
          requesty=int(random(height-100));
        else if (y<requesty)
          y+=speedY;
        else if (y>requesty)
          y-=speedY;
        else if (y==requesty)
          requesty=int(random(height-100));
      }
    }
    
    
    class Rocks {
      private int x, y;
      Rocks(int xpos, int ypos) {
        x=xpos;
        y=ypos;
      }
      void display() {
        int xRocks=int(random(width));
        int yRocks=int(random(height-25, height));
        noStroke();
        int w=int(random(5));
        //if w==0 then it will fill the color of bubbles
        if (w==1)
          fill(#37EA1C);
        else if (w==2)
          fill(#FFEB08);
        else if (w==3)
          fill(#08F9FF);
        else if (w==4)
          fill(#FF0808);
        else if (w==5)
          fill(#FF08B9);
        ellipse(xRocks, yRocks, 10, 10);
      }
    }
    
    class Booty { //draws booty
      private int x, y;
      public Booty(int xpos, int ypos) {
        x=xpos;
        y=ypos;
      }
      void display() {
        image(booty, x, y, 50, 50);
      }
    }
    
  • Answer ✓

    Congratulations!

    ;-)

Sign In or Register to comment.