mouseButton == LEFT updates 1 frame after click?

Hey guys,

as the title suggested. i'm using mouseButton == LEFT and RIGHT. When i press one of the buttons nothing happens at first but when i click a second time the screen does wat the first click should have done.

my thoughts are that it has to do something with order ofmy code. but i'm not sure this is the problem.

first of i have a mousePressed in my main file.

void mousePressed(){ startSpel(); schermWissel(spelStatus); }

This has a startSpel(); function (This starts the game i'm dutch so all my functions are in dutch so dont get confused:P)

void startSpel(){ if(mouseX > width/2 - (width * 0.5 / 2) && mouseX < width/2 + (width * 0.5 / 2) && mouseY > height * 0.8 - 30 && mouseY < height * 0.8 + 30 && spelStatus == 1){ spelStatus = 2; println("start spel fired"); } }

and a function that switches between screens.

void schermWissel(int spelStatus){
    switch(spelStatus){
      case 1: tekenStartScherm(xAs, yAs);
      break;

      case 2: tekenSpel(xAs,yAs, score); 
              tekenVeld(xAsCell,yAsCell, bommenArray, breedteCell, grootteVeld);
              tekenBommenVeld(xAsCell,yAsCell, bommenArray, breedteCell, vakkenArray);
      break;

      case 3: gameOver();
      break;

      case 4: winScherm();
    }
}

The startSpel() function fires with no problem. probably because its directly inside the mousePressed() function.

But with the following function (which is fired when spelStatus is 2) when i click the left mouse button the bommenArray[i][j] changes from 1 tot 2 with no problem and the and spelStatus changes to 3 (checked it with the println(game over in functie fired); but the screen does change... only when i click again it changes. so there's a delay somewhere.

void tekenBommenVeld(float xAsCell, float yAsCell, int[][] bommenArray, float breedteCell, int[][] vakkenArray){
  for(int i = 0; i < bommenArray.length; i++){
    yAsCell = yAsCell + ((i + 1) * yAsCell) + 25;
    for(int j = 0; j < bommenArray[i].length; j++){
        xAsCell = breedteCell;
        xAsCell = xAsCell + (j * xAsCell) + 300;

        tekenVakken(xAsCell, yAsCell,  vakkenArray, i, j);
        bomKlik(xAsCell,yAsCell,breedteCell,i,j);

        if(bommenArray[i][j] == 1){
          fill(ROOD);
          rect(xAsCell, yAsCell, breedteCell -1, breedteCell -1);
        }
        else if(bommenArray[i][j] == 2){
          fill(ROOD);
          rect(xAsCell, yAsCell, breedteCell -1, breedteCell -1);
          spelStatus = 3;
          println("game over fired");
        }
        else if(bommenArray[i][j] == 3){
          fill(GROEN);
          rect(xAsCell, yAsCell, breedteCell -1, breedteCell -1);
        }
   }
   yAsCell = breedteCell;
  }
}



void bomKlik(float xAsCell, float yAsCell, float breedteCell, int i, int j){
  float straal = breedteCell / 2;

      if(mouseButton == LEFT){
        if(mouseX > xAsCell - straal && mouseX < xAsCell + straal && mouseY > yAsCell - straal && mouseY < yAsCell + straal && spelStatus == 2){
          if(bommenArray[i][j] == 1){
              bommenArray[i][j] = 2;
              spelStatus = 3;
              println("game over in functie fired");

           }
        }
      }
      if(mouseButton == RIGHT){
         if(mouseX > xAsCell - straal && mouseX < xAsCell + straal && mouseY > yAsCell - straal && mouseY < yAsCell + straal && spelStatus == 2){
           if(bommenArray[i][j] == 1){
             bommenArray[i][j] = 3;
             flaggen++;
             score++;
           }
           else if(bommenArray[i][j] == 3){
             bommenArray[i][j] = 1;
             flaggen--;
           }
         }
      }
}

Maybe its because the mouseButton == LEFT or RIGHT has to be called in the mousePressed() function? i'm hoping thats not it.

does anybody have any idea what is should do.

Tagged:

Answers

  • Edit your post and format the code better please

  • eeeewww my bad, didn't see that i messed up the formatting

  • Without knowing your draw() or entire code:

    I like that you have spelStatus

    It should play an important role in draw() AND in mousePressed also with switch.

    Because what you have in mousePressed is also state depending

  • can i pm you the entire code? its for a school project and i know that my fellow students are on this forum to :P

  • Answer ✓

    I am not at home

Sign In or Register to comment.