How to check a function for every single array

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:

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:

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:

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:

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!

You are doing it wrong.


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


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


  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


     ................../// 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 ?

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

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