# How to check a function for every single array

**URL:** https://discourse.processing.org/t/how-to-check-a-function-for-every-single-array/25444
**Category:** Coding Questions
**Created:** [November 15, 2020, 8:06pm UTC](https://discourse.processing.org/t/how-to-check-a-function-for-every-single-array/25444 "2020-11-15T20:06:44Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![tea](https://avatars.discourse-cdn.com/v4/letter/t/7ea924/32.png) [@tea](https://discourse.processing.org/u/tea)
#### Post date: [November 15, 2020, 8:06pm UTC](https://discourse.processing.org/t/how-to-check-a-function-for-every-single-array/25444/1 "2020-11-15T20:06:44Z")

</div>

So I’m trying to make a platformer game where the platform is created in the set up in an array, and then I check the collision of the character and the platform with another function, like so:

In the setup:

```auto
void setup () {
p = new Platform [5]; //create array [number of platforms]
    p[0] = new Platform (0, 8*s, 11.5*s, 3*s); //draw platform
    p[1] = new Platform (12.5*s, 7.75*s, s, 0.25*s); 
    p[2] = new Platform (14.5*s, 7.5*s, s, 0.25*s); 
    p[3] = new Platform (16.5*s, 7.25*s, s, 0.25*s); 
    p[4] = new Platform (18.5*s, 7*s, s, 0.25*s);
}

```

In the Platform Class:

```auto
class Platform {
  float x, y, w, h;

  Platform (float xpos, float ypos, float wsize, float hsize) {
    x = xpos;
    y = ypos;
    w = wsize;
    h = hsize;
  }

  void displayplatform () {
    rect (x + worldx, y, w, h);
  }
}

```

In the draw:

```auto
void draw () {
  for (int i = 0; i < p.length; i++) { //for all platforms
    p[i].displayplatform ();
    platformcollision (b, p[i]); //check boy and platform position
    col = i;
  }
  boycollision (b, p[i]);
}

```

And the collision:

```auto
void collisionplatform (Boy b, Platform p) {
  if (b.x + 100 >= p.x && keymove[0] == 1) { // left of boy touches right of platform AND he's heading left
    collideType = 0;
  } else if (b.x < p.x + p.w && keymove[0] == 1) { // right of boy touches left of platform AND he's heading right
    collideType = 1;
  } else if (b.y < p.y + p.h && b.yspd != 0) { // top of boy touches bottom of platform AND he's jumping
    collideType = 2;
  } else if (b.y + 100 > p.y && b.yspd == 0) { // bottom of boy touches top of platform AND he's not jumping (...?)
    collideType = 3;
  }
}

void boycollision (Boy b, Platform p) {
  if (collideType == 0) {
    //move boy position back
    b.x = p.x + p.w;
  } 
  if (collideType == 1) {
    b.x = p.x - 100;
  } 
  if (collideType == 2) {
    b.y = p.y + p.h;
  } 
  if (collideType == 3) {
    b.y = p.y - 100 + worldx; //bottom of boy touches top of platform, let him run around
  }
}

```

And for reference, keymove [0] happens when the left key is pressed, and keymove [1] happens when the right key is pressed, and it controls the worldx function, which moves the world left and right. A bit patchy, but it works.

My logic is that every collision type (top of boy touches bottom of platform, for example) has its own “what happens if that happens” (for example above, you send the position of the boy to before he touches the platform, or he can’t get through the platform).

Everything runs just fine up until I write the collision function. Then the character keeps glitching everywhere. I tried using println for each collideType, which is stuck looping between collideType2 and collideType3.

I have tried a few other things, but this keeps happening. Honestly, I’m at loss at where I went wrong here. I tried to figure out where I went wrong, but after some time I just need to get advice. Any help or pointers is really appreciated. Thank you in advance!

---

<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: [November 15, 2020, 8:24pm UTC](https://discourse.processing.org/t/how-to-check-a-function-for-every-single-array/25444/2 "2020-11-15T20:24:26Z")

</div>

You are doing it wrong.

a)

Is `collideType ` initially == -1? Because when it’s 0, you will always have a collision type 0

b)

when you have a collision you set `collideType `; but then you check the next platform and maybe there is also a collision?

You could let `collisionplatform ` return a boolean indicating whether we had a collision here

c)

```auto
  boycollision (b, p[i]);

```

is after the for loop; you want to evaluate the global `collideType `, so it’s okay, that’s after the for-loop } but then store your i in the for-loop as i\_Collision and use i\_Collision after the for-loop

Chrisir

---

<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: [November 15, 2020, 8:28pm UTC](https://discourse.processing.org/t/how-to-check-a-function-for-every-single-array/25444/3 "2020-11-15T20:28:49Z")

</div>

> [@tea](#):
>
> which is stuck looping between collideType2 and collideType3.

```auto
     ................../// top of boy touches bottom of platform AND he's jumping
    collideType = 2;

```

when he jumps against a platform from underneath it (“top of boy touches bottom of platform”), then `yspd ` WAS negative and should get positive now in the function `boycollision ` ?

> [@tea](#):
>
> ```auto
> .........................b.yspd == 0) { // bottom of boy touches top of platform AND he's not jumping (...?)
> collideType = 3;
> 
> ```

When he FALLS (“bottom of boy touches top of platform”) and lands on a platform `yspd ` WAS pos and should get 0 now in the function `boycollision ` ?  
I am not sure why you check for `yspd==0` though

---

<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: [November 15, 2020, 8:31pm UTC](https://discourse.processing.org/t/how-to-check-a-function-for-every-single-array/25444/4 "2020-11-15T20:31:31Z")

</div>

to get real help please post a runnable version (post entire Sketch in ONE GO)
