Some problems with 2d array

i create 2d array of map, to create a platformer game, and map is not render correctly

platform pl;

FloatList plx, ply;

int[][] map = {
  {0, 1, 1, 0}, 
  {0, 0, 0, 1}, 
  {0, 1, 1, 0}, 
  {0, 0, 0, 1}
};

void setup() {
  plx = new FloatList();
  ply = new FloatList();
  for (int i = 0; i < 4; i++) {
    for (int j = 0; j < 4; j ++) {
      if (map[j][i] == 1) {
        plx.append(i*100);
        ply.append(j*50);
      }
    }
  }

  size(400, 400);
  for (int i = 0; i < plx.size(); i++)
    pl = new platform(100, 50);
  print(plx, ply);
}

void draw() {
  rectMode(CENTER);
  background(200);
  for (int i = 0; i < plx.size(); i++) {
    for (int j = 0; j < ply.size(); j++) {
      pl.show(plx.get(i), ply.get(j));
    }
  }
}

class platform {
  float Width;
  float Height;

  platform(float w, float h) {
    Width = w;
    Height = h;
  }

  void show(float x, float y) {
    fill(40, 40, 150);
    rect(x, y, Width, Height);
    noFill();
  }
}

What do you see now and what do you want to see instead?

@Chrisir
map as in array

Step 1

you don’t need a nested (double) for loop in draw(), because you have all x and y data in the FloatLists. They are 1D arrays (not 2D arrays) so you can use them with a simple for-loop:

platform pl;

FloatList plx, ply;

int[][] map = {
  {0, 1, 1, 0}, 
  {0, 0, 0, 1}, 
  {0, 1, 1, 0}, 
  {0, 0, 0, 1}
};

void setup() {

  size(1400, 400);

  plx = new FloatList();
  ply = new FloatList();

  for (int i = 0; i < 4; i++) {
    for (int j = 0; j < 4; j ++) {
      if (map[j][i] == 1) {
        plx.append(i*100+50); // adding + 50 here 
        ply.append(j*50+50); // adding + 50 here
      }
    }
  }

  for (int i = 0; i < plx.size(); i++) {
    pl = new platform(100, 50);
  }

  print(plx, ply);
}

void draw() {
  rectMode(CENTER);
  background(200);
  for (int i = 0; i < plx.size(); i++) {  // ONE simple for-loop, not a nested for-loop
    pl.show(plx.get(i), ply.get(i));
  }
}


// ===================================================================

class platform {

  float Width;
  float Height;

  platform(float w, float h) {
    Width  = w;
    Height = h;
  }

  void show(float x, float y) {
    fill(40, 40, 150);
    rect(x, y, Width, Height);
    noFill();
  }
}
//

Step 2

Generally speaking you make this too complicate.

The goal of having a Platform class is that you make a list of the platforms (ArrayList) and
store the data of position (x and y) also INSIDE the class.
Then you don’t need the two Floatlists at all.

// we make a list platforms and tell it that it contains objects of type Platform
ArrayList<Platform> platforms = new ArrayList();

void setup() {

  size(1400, 400);

  int[][] map = {
    {0, 1, 1, 0}, 
    {0, 0, 0, 1}, 
    {0, 1, 1, 0}, 
    {0, 0, 0, 1}
  };

  for (int i = 0; i < 4; i++) {
    for (int j = 0; j < 4; j ++) {
      if (map[j][i] == 1) {
        Platform pl = new Platform(i*100+50, j*50+50, 100, 50);
        platforms.add(pl);
      }//if
    }//for
  }//for
}

void draw() {
  background(200);

  for (int i = 0; i < platforms.size(); i++) {  // ONE simple for-loop, not a nested for-loop
    platforms.get(i).show();
  }
}


// ===================================================================

class Platform {

  float x; 
  float y; 
  float Width;
  float Height;

  Platform(float x_, float y_, 
    float w, float h) {
    x=x_;
    y=y_;
    Width  = w;
    Height = h;
  }

  void show() {
    fill(40, 40, 150);
    rectMode(CENTER);
    rect(x, y, Width, Height);
    noFill();
  }
}//class
//

Warm regards,

Chrisir

1 Like

@Chrisir
thanks its works

1 Like