Question

I'm trying to make a tetris game for android to help learn game programming for android. My goLeft/Rights break right when the button is pressed, the code for going left is in a class separate of the fields int array, and the list parts array. The fields array is accessed by a referenced variable (TetrisWorld tetrisworld;). While part list array is public so accessed through a variable(part) code for which is in the goLeft() code. It breaks at: if(tetrisworld.fields[x][part.y] != 0) Code for left:

public void goLeft() {
    int x = 0;
    for(int i = 0; i < 4; i++) {
        TetrisParts part = parts.get(i);
        x = part.x - 1;
        if(tetrisworld.fields[x][part.y] != 0) {
            noleft = true;
            break;
        }
    }
    if(noleft == false) {
        for(int i = 0; i < 4; i++) {
            TetrisParts part = parts.get(i);
            part.x--;
        }
    }
}

The code for the fields int array:

int fields[][] = new int[WORLD_WIDTH][WORLD_HEIGHT];

WORLD_WIDTH and WORLD_HEIGHT are both static final ints, width being 9 and height being 19

I've tried putting if(tetrisworld.fields[0][0] == 0) and it still crashes so I don't think it has to do with the variables. Also It doesn't go out of bound even if I haven't added the code to check for that yet because I have the teroid spawning around x = 5 and since I can't go left/right once there's not a chance of that happening

I've tried moving the goLeft/Right methods to the gamescreen class which has a "world = TetrisWorld();" and it still bugs out at the same spot

UPDATE:

Ok just adding:

tetrisworld != null

to the first if statement fixed it, my question now is, why did it fix it? Why can't I move without this check? It clearly isn't null cause as far as I know; it's fully responsive now.

But an easier way to have solved this which is SOOOO easy is changing fields to static... then access it lika so: TetrisWorld.fields so my updated code is:

public void goLeft()
  {
  noleft = false;
  for (int i = 0; i < 4; i++)
     {
     part = parts.get(i);
     if (part.x - 1 < 0 || TetrisWorld.fields[part.x - 1][part.y] != 0)
        {
        noleft = true;
        break;
        }
     }
  if (noleft == false)
     {
     for (int i = 0; i < 4; i++)
        {
        part = parts.get(i);
        part.x--;
        }
     }
  }

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top