Create and display 3 platforms?

hey there! new user here!

I’ve been following the book Learning Processing for a few months, and have finally arrived at the algorithms part. I wanted to try and use more less everything I’ve learned in coding a small platform game, in which there was a playable character which you can move with WASD and a target that you could move with arrowkeys and could, by pressing the E key, create a platform onto witch the character could jump and stay

So once i got all that (still didnt get how to do the gravity thing, but anyways) I wanted to try and implement an array of 3 platforms in which you could create up to 3 platforms to jump on from one to another (so like you press E one time, you create platform1, then the second you create platform2, and then platform3, and then back to platform 1 and so on. The E button just changes the coords of the platform and overwrites them with those from the target. The problem is that for each platform the code is this:

class Platform {

  float x;
  float y;
  float largo;
  float altura;
  color col;

  Platform() {
    x=1000;
    y=1000;
    altura=10;
    largo=60; 
    col=color(255, 255, 0);
  }


  void create() {
    x = target.x;
    y = target.y;

    println(target.x);
  }
  void display() {
    fill(200, 0, 255);
    rectMode(CENTER);
    rect(x, y, largo, altura);
  }
}

And the main program goes like this:

Player pj;
Target target;
Platform platform;
Platform platform2;
Platform platform3;

int index = 0;

void setup () {
  size (600, 300);
  pj = new Player();
  target = new Target();
  platform = new Platform();
  platform2 = new Platform();
  platform3 = new Platform();
}


void draw() {
  background(0);
  platform.display();
  platform2.display();
  platform3.display();
  pj.display();
  target.display();
  println(index);
}



void keyPressed() {
  if (key == CODED) {
    //para mover el pj

    if (keyCode == UP) {
      pj.col = color(0, 0, 255);
      pj.y -= 10;
    } else if (keyCode == DOWN) {
      pj.col = color(255, 0, 0);
      pj.y += 10;
    } else if (keyCode == RIGHT) {
      pj.col = color(255);
      pj.x += 10;
    } else if (keyCode == LEFT) {
      pj.col = color(255);
      pj.x -= 10;
    }
  }

  //target

  if (key == 'w' || key == 'W') {
    target.y -= 10;
  } else if (key == 's' || key == 'S') {
    target.y += 10;
  } else if (key == 'd' || key == 'D') {
    target.x += 10;
  } else if (key == 'a' || key == 'A') {
    target.x -= 10;
  }

  //platform thingie
  if (key == 'e' || key == 'E') {
    if (index == 0) {
      platform.create();
      println("PL1");
      index = index + 1;
    }

    if (index == 1) {
      platform2.create();
      println("PL2");
      index = index + 1;
    }

    if (index == 2) {
      platform3.create();
      println("PL3");
      index = index + 1;
    }
    if (index == 3) {
      index = 0;
    }
  }
}

Also here is the code for the playable character:

class Player {

  float x;
  float y;
  float gravity;
  float acc;
  float altura;
  color col;


  Player() {
    x=20;
    y=20;
    altura=50;
    gravity=0.08; 
    acc=0.08;
    col=color(255);
  }

  void display() {
    rectMode(CENTER);
    fill(col);
    rect(x, y, 20, altura);

    y=y+gravity;
    if (y>height-altura/2) {
      gravity = 0;
    } else if (y<height-altura/2) {
      gravity =0.08;
    }



    if ((x<platform.x+30 && x>platform.x-30)) {
      gravity = 0;
    }
    if ((x<platform2.x+30 && x>platform2.x-30)) {
      gravity = 0;
    }
    if ((x<platform3.x+30 && x>platform3.x-30)) {
      gravity = 0;
    }
  }
}

and that for the target:

 class Target {

  float x;
  float y;
  color col;

  Target() {
    x=500;
    y=50;
    col = color (255, 255, 0);
  }

  void display() {
    ellipseMode(CENTER);
    rectMode(CENTER);
    fill(col);
    ellipse(x, y, 5, 5);
    rect (x-10, y, 5, 2);
    rect (x+10, y, 5, 2);
    rect (x, y-10, 2, 5);
    rect (x, y+10, 2, 5);
  }
}

I try to test things using the modulo, but still cant get the platforms from appearing the 3 at the same time. What did I did wrong?

Thank you and sorry for such a long first post!

1 Like

Hi, there is a simple answer for your problem:
index = 0 at the beginning so the first if is executed.
at the end of the if index = 1 is set.
then the second if is checked and guess what, index == 1 is true.
all your ifs are executed after one another.

To fix it, use else if:

if (index == 0) {
      platform.create();
      println("PL1");
      index = index + 1;
    } else if (index == 1) {
      platform2.create();
      println("PL2");
      index = index + 1;
    } else if (index == 2) {
      platform3.create();
      println("PL3");
      index = index + 1;
    }
    
    if (index == 3) {
      index = 0;
    }

Alternativly you could also not count up in the if() itself and do it after all if-tests for all cases in common.

Yay! Thank you! Problem solved!