# Problem with translate() in class when using arraylist

**URL:** https://discourse.processing.org/t/problem-with-translate-in-class-when-using-arraylist/31732
**Category:** Coding Questions
**Created:** [August 15, 2021, 10:06am UTC](https://discourse.processing.org/t/problem-with-translate-in-class-when-using-arraylist/31732 "2021-08-15T10:06:14Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![chaotic.rabbit](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chaotic.rabbit/32/14034_2.png) [@chaotic.rabbit](https://discourse.processing.org/u/chaotic.rabbit)
#### Post date: [August 15, 2021, 10:06am UTC](https://discourse.processing.org/t/problem-with-translate-in-class-when-using-arraylist/31732/1 "2021-08-15T10:06:15Z")

</div>

Hi, I really need help with this code. I’ve been trying to make infinite jellyfish appear from the bottom of the screen and move upwards until they disappear. For some reason after a while the jellyfish start appearing from the middle of the screen even though I set the translation to more than 800 and only 4 of them appear. I’m thinking that more are being added too far up the screen so they’re not visible. Below is the code:

```auto

float xSpeed;
float ySpeed;
float ellipseX = 500;
float ellipseY = 550;
ArrayList<Jellyfish> jellies = new ArrayList<Jellyfish>();
ArrayList<Jellyfish> visibleJellies = new ArrayList<Jellyfish>();
float ty = 500;
int time=0;
int counter;

void setup(){
 counter=0;

for(int i =0; i<100; i++){
  jellies.add(new Jellyfish(random(0, 1500), 850, random(-40, 40), random(0.05, 0.35)));
}
 size(1500, 850);

void draw() {
  background(8, 22, 46);
  for (int s = 0; s < stars.length; s++) {    
    stars[s].display();
  }
  image(b, 0, 0);
  if (millis()>time+2000) {
    time=millis();
    visibleJellies.add(jellies.get(counter));
    if (counter < 100) counter++;
    else counter = 0;
  }

  for (int i = 0; i < visibleJellies.size(); i++) {
    visibleJellies.get(i).display();
  }
}

class Jellyfish{
  float transX;
  float transY;
  float r;
  float s;

  
Jellyfish(float transX, float transY, float r, float s){
this.transX = transX;
this.transY = transY;
this.r = r;
this.s = s;

}

void display(){
  pushMatrix();
  translate(transX, transY);
  rotate(radians(r));
  scale(s);
  strokeWeight(5);
  fill(5, 29, 242, 40);
  stroke(5, 29, 242);
  xSpeed += 0; 
  ySpeed -= random(5, 6);

 
  
  beginShape();
  strokeWeight(12);
  
  arc(500+xSpeed, 500+ySpeed, 500, 500, -PI, 0);
  endShape();
  //tentacle1
  beginShape();
  
  
  noFill();
  curveVertex(500+xSpeed, ty+ySpeed); 
  curveVertex(500+xSpeed, ty+ySpeed); 
  curveVertex(600+xSpeed, ty+250+ySpeed);
  curveVertex(550+xSpeed, ty+350+ySpeed);
  curveVertex(475+xSpeed, ty+400+ySpeed); 
  endShape();
  
  //tentacle2
  beginShape();
 
  
  noFill();
  curveVertex(375+xSpeed, ty+ySpeed); 
  curveVertex(375+xSpeed, ty+ySpeed); 
  curveVertex(325+xSpeed, ty+100+ySpeed);
  curveVertex(350+xSpeed, ty+325+ySpeed);
  curveVertex(250+xSpeed, ty+350+ySpeed); 
  endShape();
  
  //tentacle3
  beginShape();
  
  
  noFill();
  curveVertex(650+xSpeed, ty+ySpeed); 
  curveVertex(650+xSpeed, ty+ySpeed); 
  curveVertex(650+xSpeed, ty+100+ySpeed);
  curveVertex(700+xSpeed, ty+325+ySpeed);
  curveVertex(550+xSpeed, ty+350+ySpeed); 
  endShape();
  

  popMatrix();
}
}

```

---

<div class="post-metadata">

### Author: ![micuat](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/micuat/32/19407_2.png) [@micuat](https://discourse.processing.org/u/micuat)
#### Post date: [August 15, 2021, 11:25am UTC](https://discourse.processing.org/t/problem-with-translate-in-class-when-using-arraylist/31732/2 "2021-08-15T11:25:30Z")

</div>

Hi! First of all, nice drawing! I want to point out that the code you pasted has some errors (setup isn’t closed, there are some missing variables like `stars` and `b`… which I assume are not relevant to your question).

I think you are almost there, but since `ySpeed` is declared globally and not for each jellyfish, the position is applied to all the jellyfish.

Some other suggestions:

- instead of using `xSpeed` and `ySpeed` in each draw (arc/vertex/etc), you can skip these values and instead use it once in `translate` just before the first `beginShape`, which will make the code more concise.
- rather a small point but what you mean by `xSpeed` and `ySpeed` is not speed but in fact, position…
- what happens after 100 jellyfish appear? Actually, it will give an error here:

```auto
    if (counter < 100) counter++;
    else counter = 0;

```

because this permits `counter` to be 100. What you want instead is

```auto
    counter++;
    if (counter >= jellies.size()) counter = 0;

```

but still the code won’t work as you expect because old jellyfish won’t be initialized (they are already outside the top edge, and you want to give life to this jellyfish again). There are a few ways to fix this so that the new jellyfish will appear at the bottom. One is to naively instantiate a new jellyfish object instead of moving from `jellies` to `visibleJellies`. Another (a bit more efficient) way would be to add an `init` function to reset the position of the “old” jellyfish - but in this case, you should not `visibleJellies.add` again because the same object will be added in the ArrayList over and over and end up in a weird situation (try `millis()>time+2` so you don’t have to wait for 3 minutes for debugging) …

---

<div class="post-metadata">

### Author: ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)
#### Post date: [August 15, 2021, 12:00pm UTC](https://discourse.processing.org/t/problem-with-translate-in-class-when-using-arraylist/31732/3 "2021-08-15T12:00:04Z")

</div>

One of errors size is not at first setup

```auto
void setup(){
 size(1500, 850);
 counter=0;

for(int i =0; i<100; i++){
  jellies.add(new Jellyfish(random(0, 1500), 850, random(-40, 40), random(0.05, 0.35)));
}
 
}

```

And take in consideration Random return Float

---

<div class="post-metadata">

### Author: ![chaotic.rabbit](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chaotic.rabbit/32/14034_2.png) [@chaotic.rabbit](https://discourse.processing.org/u/chaotic.rabbit)
#### Post date: [August 16, 2021, 4:43pm UTC](https://discourse.processing.org/t/problem-with-translate-in-class-when-using-arraylist/31732/4 "2021-08-16T16:43:33Z")

</div>

Hello, many thanks for this detailed answer. Yes my problem is that I am trying to instantiate a new jellyfish object so that new ones appear from the bottom of the screen but for some reason by the 3rd one they start appearing in the middle of the screen and I have no idea how to solve it. I want new ones to appear from the bottom of the screen every x seconds. Thanks

---

<div class="post-metadata">

### Author: ![micuat](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/micuat/32/19407_2.png) [@micuat](https://discourse.processing.org/u/micuat)
#### Post date: [August 16, 2021, 5:16pm UTC](https://discourse.processing.org/t/problem-with-translate-in-class-when-using-arraylist/31732/5 "2021-08-16T17:16:56Z")

</div>

?  
I understood your question and answered in the post above 🙂

---

<div class="post-metadata">

### Author: ![chaotic.rabbit](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chaotic.rabbit/32/14034_2.png) [@chaotic.rabbit](https://discourse.processing.org/u/chaotic.rabbit)
#### Post date: [August 16, 2021, 5:43pm UTC](https://discourse.processing.org/t/problem-with-translate-in-class-when-using-arraylist/31732/6 "2021-08-16T17:43:09Z")

</div>

yes I’ve already tried adding new jellyfish but the jellyfish still start appearing from the middle of the screen after the 2nd/3rd one…that’s why I attempted to use the visiblejellies but still didn’t work.

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [August 16, 2021, 6:23pm UTC](https://discourse.processing.org/t/problem-with-translate-in-class-when-using-arraylist/31732/7 "2021-08-16T18:23:26Z")

</div>

micuat pointed out a few mistakes of yours

Did you correct them all?

If you have please post your entire code again otherwise read his post again and keep on working on your Sketch

---

<div class="post-metadata">

### Author: ![micuat](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/micuat/32/19407_2.png) [@micuat](https://discourse.processing.org/u/micuat)
#### Post date: [August 16, 2021, 6:48pm UTC](https://discourse.processing.org/t/problem-with-translate-in-class-when-using-arraylist/31732/8 "2021-08-16T18:48:36Z")

</div>

> [@micuat](#):
>
> I think you are almost there, but since `ySpeed` is declared globally and not for each jellyfish, the position is applied to all the jellyfish.

here’s the answer - in case you missed it

---

<div class="post-metadata">

### Author: ![chaotic.rabbit](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chaotic.rabbit/32/14034_2.png) [@chaotic.rabbit](https://discourse.processing.org/u/chaotic.rabbit)
#### Post date: [August 26, 2021, 8:46am UTC](https://discourse.processing.org/t/problem-with-translate-in-class-when-using-arraylist/31732/9 "2021-08-26T08:46:41Z")

</div>

I must have been doing something wrong (sorry I’m only a beginner). Many thanks for your help, I managed to make it work eventually 🙂
