Problem with the if condition

edited May 2016 in Library Questions

Hello guys, I'm currently making a Memory game. I'm having a problem when I want to go to the next screen (I'm having the main screen, with mouse click I go to the next creen and so on....) everything is fine until I go to a specific screen, click how many cards I want to have and it directly jumps to the game. But there's another screen between them so the problem is that it skips a screen. Here's the code for the buttons screen which skips the next screen

 if (isPointOnButton3==true)
{
  cardCount=1;
  state=4;
} else if (isPointOnButton4==true)
{
  cardCount=2;
  state=4;
} else if (isPointOnButton5==true)
{
  cardCount=3;
  state=4;

} else if (isPointOnButton6==true)
{
  cardCount=4;
  state=4;
}
}
}

And here's the skipped screen

void ZmenRub()
{
if (state==4)  //zvolení počtu dvojic
{

fill(0);
background(204);
textFont(myFont2);
text("Zvolte rub karet", 335, 260);
volbaK = loadImage("00cards.png");
volbaK2 = loadImage("001cards.png");
volbaK3 = loadImage("002cards.png");
volbaK4 = loadImage("003cards.png");
volbaK5 = loadImage("004cards.png");
volbaK6 = loadImage("005cards.png");
image(volbaK, 190, 365, 100, 150);
image(volbaK2, 290, 365, 100, 150);
image(volbaK3, 390, 365, 100, 150);
image(volbaK4, 490, 365, 100, 150);
image(volbaK5, 590, 365, 100, 150);
image(volbaK6, 690, 365, 100, 150);






if (isPointOnButton7==true)
{


  gcardback = loadImage("00cards.png");
  InicializaceKaret();
  file.stop();
  file = new SoundFile(this, "menu.mp3");
  file.play();

  state=5;
} else if (isPointOnButton8==true)
{


  gcardback = loadImage("001cards.png");
  InicializaceKaret();
  file.stop();
  file = new SoundFile(this, "menu.mp3");
  file.play();

  state=5;
} else if (isPointOnButton9==true)
{


  gcardback = loadImage("002cards.png");
  InicializaceKaret();
  file.stop();
  file = new SoundFile(this, "menu.mp3");
  file.play();

  state=5;
} else if (isPointOnButton10==true)
{

  gcardback = loadImage("003cards.png");
  InicializaceKaret();
  file.stop();
  file = new SoundFile(this, "menu.mp3");
  file.play();

  state=5;
} else if (isPointOnButton11==true)
{

  gcardback = loadImage("004cards.png");
  InicializaceKaret();
  file.stop();
  file = new SoundFile(this, "menu.mp3");
  file.play();

  state=5;
} else if (isPointOnButton12==true)
{

  gcardback = loadImage("005cards.png");
  InicializaceKaret();
  file.stop();
  file = new SoundFile(this, "menu.mp3");
  file.play();

  state=5;
}
}
}

I don't know why it skips the screen, the if condition is not wrong. Thanks in advance for your help and tips

Tagged:

Answers

  • edited May 2016

    could you post your entire code, please?

    in short, it seems not wise to use loadImage() outside setup()

    also, it is suspicious that you use isPointOnButton1 to isPointOnButton12.... what are those? Selector of how many Cards you want to have in the game? The actual memory cards of the game with the images? Then you are doing it wrong.

    what is InicializaceKaret() doing?

    hint:

    When ZmenRub() is called from draw() and the state is set to 5 (!) there and then state 5 gets drawn, you'll never see 4 since the screen is updated only at the end of draw() once and not throughout (and draw() runs 60 times per second).

    Instead use a timer and set state to 5 later.

    //
    // states
    final int stateGame  = 0; // consts
    final int statePause = 1;
    int state = stateGame;    // current 
    
    int timerStart;
    // -------------------------------------------------
    
    void setup() {
      // init (runs only once)
      size(800, 600);
      timerStart=millis();
    } // func 
    
    void draw() { 
      // runs on and on 
    
      switch (state) {
    
      case stateGame: 
        // Game
        handleStateGame();
        break; 
    
      case statePause:
        // Pause
        handleStatePause(); 
        break;
    
      default:
        // error
        println("Error number 939; unknown state : " 
          + state
          + ".");
        exit();
        break;
      } // switch
      //
    } // func 
    
    // functions for states - called from draw() 
    
    void handleStateGame() {
      // Game
      background(11);
      fill(244, 3, 3); // red 
      text ("Waiting for timer.......", 210, 313);
    
      //
      noStroke();
      fill(255, 2, 2) ;
      rect(100, 100, 100, 100);
      fill(255, 2, 255) ;
    
      if (timerStart+1200<millis()) {
        state = statePause;
      }
    } // func 
    
    void handleStatePause() {
      // Pause
      background(255);
      fill(244, 3, 3); // red 
      text ("Timer has gone off....", 210, 313);
    } // func 
    // =========================
    
  • Hey thanks for the help. Is it possible to send you the whole package with the images and sounds, cause posting the whole code here is troublesome

  • i wrote a wrong sentence and corrected it now

    correct is:

    in short, it seems not wise to use loadImage() outside setup()

  • So I just replaced the ifs with a switch

      void draw()
        {
         switch (state) {
    
         case stateMain:
        MainMenu();
        break;
    
        case statePCount:
        PlayerChange();
        break;
    
        case stateCCount:
        CardCount();
        break;
    
        case stateCImage:
        CardBack();
        break;
    
        case stateGame:
        Game();
        break;
    
       }
    
    
    
    
     }
    

    but the result is the same :(

  • Much better

    As I said, the issue is that you set state to 5 in state 4 - thus you jump over state 4

    I PM you

  • Solved

Sign In or Register to comment.