Glitching, code not responding to commands.

I have a game that is supposed to go to different screens when certain commands are issued. For instance, after winning "playstate2", I'm supposed to go to the homescreen so I could restart the game if I want to. However, the code sends me to a different screen and I cannot start another game. I don't understand why but I think it's an encapsulation problem but I'm not sure. Can anyone help?

//First declare objects used for this session

// Level 1 will have 2 blockers 
// Level 2 will have 3 blockers w/ 1 going faster. 
Blocker obstacleBlocktopdown; 
Blocker obstacleBlockacross;
Blocker obstacleBlocklvl2_rocket;
Blocker obstacleBlocklvl2_uno;
Blocker obstacleBlocklvl2_dos;
Avatar playerAvatar;
Avatar playerAvatar2;
Seeker seekerStone1; 
Seeker seekerStone2; 
GoalState goal; 
GoalState goal2;  



boolean upPressed = false;
boolean downPressed = false;
boolean leftPressed = false;
boolean rightPressed = false;
boolean winstate = false; 
boolean winstate2 = false; 
boolean playstate = false;
boolean playstate2 = false; 
boolean homescreen = true;
boolean tutorialscreen = false; 
boolean losestate = false; 
boolean losestate2 = false; 
boolean pausestate = false; 
boolean pausestate2 = false; 
int wincount = 0; 
int losscount = 0; 



void setup(){
  size(950, 950); 
  //////objects for playstate
  obstacleBlocktopdown = new Blocker(width/2, 1, 20, 20, 0, 1.2); 
  obstacleBlockacross = new Blocker(1, height/2, 20, 20, 2, .4); 
  playerAvatar = new Avatar(11, 11, 18, 18);  
  seekerStone1 = new Seeker(1100, 600, 18, 18); 
  goal = new GoalState(790, 500); 
  //////objects for playstate2
  playerAvatar2 = new Avatar (425, 20, 18, 18); 
  seekerStone2 = new Seeker(425, 930, 18, 18); 
  obstacleBlocklvl2_uno = new Blocker(10, 10, 20, 20, .7, 1.2);
  obstacleBlocklvl2_dos = new Blocker(940, 940, 20, 20, .5, -1.3); 
  obstacleBlocklvl2_rocket = new Blocker ( 2, height/3, 15, 15, 1.7, 1.5);
  goal2 = new GoalState(100, 20);

}


void draw(){ 
 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////HOMESCREEN  
  if (homescreen == true){
    pausestate = false; 
    background((mouseX+150)/4, (mouseY+150)/4, 0); 
    //image(titlecard, 200, 400, 500, 200); 
    stroke(0); 
    fill((mouseY)/5, 0, (mouseX)/5); 
    textSize(60); 
    text("Goal Seeker", 200, height/4); 
    stroke(0); 
    fill((mouseY -50)/3, 0, (mouseX-50)/3); 
    textSize(24); 
    text("Press Spacebar to begin", 200, height - (height/4)); 
    textSize(14); 
    text("Press T for a tutorial", 775, height - (height/4)); 


    if(key == ' ') {
      homescreen = false;
      playstate = true;
      playstate2 = false; 
    }
    if(key == 't') {
       homescreen = false;
       tutorialscreen = true; 
    }

  }
 //////////////////////////////////////////////////////////////////////////////////////////////////////TUTORIAL SCREEN  
  if (tutorialscreen == true){ 

    background(255);
    stroke(0);
    fill(0);
    textSize(24); 
    text("How to play Goal Seeker", 30, 100);
    textSize(20); 
    text("Components", 55, 150);
    textSize(18); 
    text("Avatar - The Blue Ellipse", 70, 180); 
    text("Seeker - The Green Ellipse", 70, 198);
    text("Blockers - The Red Ellipse", 70, 216); 
    text("Goal - The Grey Ellipse", 70, 234); 
    textSize(20);
    text("GamePlay", 55, 270); 
    textSize(18);
    text("*The Goal of the Game is to seek out the Grey Goal point on the screen.", 75, 300);
    text("*You, the player, control the Avatar with the directional buttons.", 75, 320);
    text("*When you move the Avatar, the Seeker moves as well but in a different way that you don't control.", 75, 340);
    text("*So you must be aware of how you are moving the Avatar and how the Seeker moves in response to ", 75, 360);
    text("your moves.", 75, 380);
    text("*When the Seeker reaches the Goal, the game is won and a win is registered on the scoresheet.", 75, 400); 
    text("*If however, the Seeker OR the Avatar hits ANY of the Blockers, the game is lost", 75, 420);
    text("and a loss is registered on the scoresheet.", 75, 440);
    text("*If at anytime you need to stop the game, you can PAUSE the game by pressing 'P'.", 75, 470); 
    text("*Good Luck!", 100, 700);  


    text("Press Q to go back to Title Screen", 200, 800);
    text("Or Press spacebar to start the Game!!", 200, 850); 

    if (key == 'q') {
     tutorialscreen = false; 
     homescreen = true;  
    }
    if (key == ' '){
     tutorialscreen = false; 
     playstate = true; 
     playstate2 = false;  
    }

  }
 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////LOSE STATE 
  if (losestate == true) {
   background(0); 
   stroke(255);
   fill(255); 
   textSize(56); 
   text("You Lost!!!", 200, height/2); 
   textSize(24); 
   text("Number of games won... " + wincount, 400, 600); 
   text("Number of games lost... " + losscount, 400, 675);
   textSize(24); 
   text("Press spacebar to try again", 200, 750); 
   text("Or press Q to quit", 200, 900); 

   if (key == ' ') {
    losestate = false;
    playstate = true; 

   }
   if (key == 'q') {
    losestate = false; 
    homescreen = true;  
   }
  }

  //////////////////////////////////////////////////////////////////////////LOSE STATE 2 BEGINS 
   if (losestate2 == true) {
   background(0); 
   stroke(255);
   fill(255); 
   textSize(56); 
   text("You Lost!!!", 200, height/2); 
   textSize(24); 
   text("Number of games won... " + wincount, 400, 600); 
   text("Number of games lost... " + losscount, 400, 675);
   textSize(24); 
   text("Press spacebar to try again", 200, 750); 
   text("Or press Q to quit", 200, 900); 

   if (key == ' ') {
    losestate2 = false;
    playstate2 = true; 

   }
   if (key == 'q') {
    losestate2 = false; 
    homescreen = true;  
   }
  }


 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////PAUSE STATE 
  if (pausestate == true) {
    background(0); 
    playerAvatar.display();
    seekerStone1.display(); 
    obstacleBlocktopdown.display();
    obstacleBlockacross.display();
    textSize(48);
    text("PAUSE", 300, 300);
    textSize(36); 
    text("Press R to resume Game", 100, 700); 
    text("Press Q to Quit and go back to Title Screen", 100, 800);


    if (key == 'r' || key == 'R'){
     pausestate = false; 
     playstate = true;  
    }

    if (key == 'q' || key == 'Q'){
     pausestate = false;
     homescreen = true;  
    }

  }

////////////////////////////////////////////////////////////////////////////////////////////////ACTIVATES PLAY STATE 1 
  if (playstate == true) {

  playerAvatar.constrainToScreen(); 


  if (upPressed) {// playerAvatar moves up 1 pixel
   playerAvatar.movesup();
  }
  if (downPressed) {//playerAvatar moves down 1 pixel
   playerAvatar.movesdown();
  }
  if (leftPressed) {//playerAvatar moves to the left 1 pixel
   playerAvatar.movesleft();
  }
  if (rightPressed) {//playerAvatar moves to the right 1 pixel
   playerAvatar.movesright();
  }

  if (key == 'p' || key == 'P') {
   playstate = false; 
   pausestate = true;  
  }


  background(0); 
  goal.goalcheck(); 
  obstacleBlocktopdown.run(); 
  obstacleBlockacross.run();
  strokeWeight(1);
  stroke(255); 
  fill(255); 
  ellipse(goal.x, goal.y, 46, 46); 
  strokeWeight(1);
  stroke(255); 
  fill(16, 49, 188); 
  textSize(14);
  text("GOAL", goal.x-20, goal.y-6); 
  goal.display();
  playerAvatar.display(); 
  seekerStone1.synch2avatar();
  }

  ///////////////////////////////////////////////////////////////////////////////////ACTIVATES PLAYSTATE 2 

  if (playstate2 == true) {
    playerAvatar2.constrainToScreen(); 


  if (upPressed) {// playerAvatar moves up 1 pixel
   playerAvatar2.movesup2();
  }
  if (downPressed) {//playerAvatar moves down 1 pixel
   playerAvatar2.movesdown2();
  }
  if (leftPressed) {//playerAvatar moves to the left 1 pixel
   playerAvatar2.movesleft2();
  }
  if (rightPressed) {//playerAvatar moves to the right 1 pixel
   playerAvatar2.movesright2();
  }

  if (key == 'p' || key == 'P') {
   playstate2 = false; 
   pausestate2 = true;  
  }

  background(0); 
  strokeWeight(1);
  stroke(255); 
  fill(255); 
  ellipse(goal2.x, goal2.y, 46, 46); 
  strokeWeight(1);
  stroke(255); 
  fill(16, 49, 188); 
  textSize(14);
  text("GOAL", goal2.x-20, goal2.y-6);
  goal2.display(); 
  goal2.goalcheck2(); 
  obstacleBlocklvl2_uno.run2(); 
  obstacleBlocklvl2_dos.run2();
  obstacleBlocklvl2_rocket.run2();
  obstacleBlocklvl2_uno.bounce(); 
  obstacleBlocklvl2_dos.bounce();
  obstacleBlocklvl2_rocket.bounce();
  playerAvatar2.display(); 
  seekerStone2.synch2avatar2();

  }

  /////////////////////////////////////////////////////////PAUSE STATE 2 
  if (pausestate2 == true) {
    background(0); 
    playerAvatar2.display();
    seekerStone2.display(); 
    obstacleBlocklvl2_uno.display();
    obstacleBlocklvl2_dos.display();
    obstacleBlocklvl2_rocket.display();
    textSize(48);
    text("PAUSE", 300, 300);
    textSize(36); 
    text("Press E to resume Game", 100, 700); 
    text("Press Q to Quit and go back to Title Screen", 100, 800);
  }


    if (key == 'e' || key == 'E'){
     pausestate2 = false; 
     playstate2 = true;  
    }

    if (key == 'q' || key == 'Q'){
     pausestate2 = false;
     homescreen = true;  
    }


  ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////CREATE WIN SCENARIO

     if (winstate == true) {

       background (0); 
       textSize(36); 
       text("You Win", 100, 100); 
       text("Seeker has reached Goal!", 100, 200); 
       textSize(24); 
       text("Number of games won... " + wincount, 100, 600); 
       text("Number of games lost... " + losscount, 100, 700);

       textSize(14); 
       text("Press spacebar to go to the next level", 100, 800); 
       text("Or press 'q' to quit and go back to the homescreen", 100, 875);  

       if (key == ' ') {
         winstate = false;
         playstate2 = true;
         playstate = false;       
       }

       if (key == 'q' || key == 'Q') {
        winstate = false; 
        homescreen = true;  
       }
      }

      //////////////////////////////////////////////////////////////WINSTATE 2 

      if (winstate2 == true) {
       background (0); 
       textSize(36); 
       text("You Win", 100, 100); 
       text("Seeker has reached Goal!", 100, 200);
       text("You have finished Goal Seeker!!", 100, 300);  
       textSize(24); 
       text("Number of games won... " + wincount, 100, 600); 
       text("Number of games lost... " + losscount, 100, 700); 
       textSize(14); 
       text("Press h to return to the homescreen", 100, 900);  

       if (key == 'h' || key == 'H') {   
         winstate2 = false;

         homescreen = true; 
          }    
       }

  }
  //enacts the movement of desired object
    void keyPressed (KeyEvent e) { 
  if (key == CODED) {
    if (keyCode == UP) {
      upPressed = true;
    }
    else if (keyCode == DOWN) {
      downPressed = true;
    }
    else if (keyCode == LEFT) {
      leftPressed = true;
    }
    else if (keyCode == RIGHT) {
      rightPressed = true;
    }
  }
}

     // stops movement once key(s) are released 
void keyReleased(KeyEvent e) {
  if (key == CODED) {
    if (keyCode == UP) {
      upPressed = false;
    }
    else if (keyCode == DOWN) {
      downPressed = false;
    }
    else if (keyCode == LEFT) {
      leftPressed = false;
    }
    else if (keyCode == RIGHT) {
      rightPressed = false;
    }
  }
} 

class Avatar {
  float x; 
  float y; 
  float a;
  float b; 
  static final color avatarColor = #537DF3, outline = 0; 


  //constructors 
  Avatar(){
  }

  Avatar (float _x, float _y, float _a, float _b) {
    x = _x;
    y = _y; 
    a = _a;
    b = _b; 
  }

  //Functions 
  void display () {
    //avatarColor = (83, 125, 243); 
    strokeWeight(1);
    stroke(outline); 
    fill(avatarColor);
    ellipse(x, y, a, b);
  }

  void movesup(){// MAY NEED IF ELSE STATEMENTS 
    constrain(y, 10, height-10);
   y = y - 2; 
  playerAvatar.display(); 
  }
  void movesdown() {
    constrain(y, 10, height-10);
    y = y + 2; 
    playerAvatar.display(); 
  }
  void movesleft() {
    constrain(x, 10, width-10);
    x = x - 2; 
    playerAvatar.display(); 
  }
  void movesright() {
    constrain(x, 10, width-10);
    x = x + 2; 
    playerAvatar.display();
  }

   void movesup2(){// MAY NEED IF ELSE STATEMENTS 
    constrain(y, 10, height-10);
   y = y - 2; 
  playerAvatar2.display(); 
  }
  void movesdown2() {
    constrain(y, 10, height-10);
    y = y + 2; 
    playerAvatar2.display(); 
  }
  void movesleft2() {
    constrain(x, 10, width-10);
    x = x - 2; 
    playerAvatar2.display(); 
  }
  void movesright2() {
    constrain(x, 10, width-10);
    x = x + 2; 
    playerAvatar2.display();
  }

  void constrainToScreen() {
  x = constrain(x, a/2.0, width  - a/2.0);
  y = constrain(y, b/2.0, height - b/2.0);
}

}
Tagged:

Answers

  • Code continued.

       class Blocker {
      //Global variables
      float x = 0;
      float y = 0;
      float a = 0;
      float b = 0;
      float speedX = 0;
      float speedY = 0;
      static final color blockerColor = #E81818, outline = 255 ; 
    
      //Constructors
      Blocker() {
      }
    
      Blocker(float _x, float _y, float _a, float _b, float _c, float _d) {
        // Blocker parameters x,y coordinates, width, height x-coordinate speed & y-coordinate speed
        x=_x;
        y=_y;
        a=_a;
        b=_b;
       speedX = _c;
       speedY = _d; 
      }
    
     // Blocker[]() {
      //}
    
      //Functions
      void display () {
        //color(232, 24, 24);
        strokeWeight(1);
        stroke(outline); 
        fill(blockerColor);
        ellipse (x, y, a, b);
      }
    
    
      void startpos(float _x, float _y, float _a, float _b) {
        x=_x;
        y=_y;
        a=_a;
        b=_b; 
      }
    
      void startspeed(float _c, float _d) {
       speedX = _c;
       speedY = _d;
      }
    
      void movement() {//make blockers move in basic motions
    
        x = x + speedX;
        y = y + speedY;
      }
    
      void bounce() {
        if (x > width) {
          speedX = speedX * -1;
        }
        if (x < 0) {
          speedX = speedX * -1;
        }
        if (y > height) {
          speedY = speedY * -1;
        }
        if (y < 0) {
          speedY = speedY * -1;
        }
      }
    
      void bounce2() {
        if (x > width) {
          speedX = speedX * -1;
        }
        if (x < 0) {
          speedX = speedX * -1;
        }
        if (y > height) {
          speedY = speedY * -1;
        }
        if (y < 0) {
          speedY = speedY * -1;
        }
      }
    
      void run() {
        obstacleBlocktopdown.display();
        obstacleBlockacross.display();
        obstacleBlocktopdown.movement();
        obstacleBlockacross.movement();
        obstacleBlocktopdown.collide();
        obstacleBlockacross.collide();
        obstacleBlocktopdown.seekercollide(); 
        obstacleBlockacross.seekercollide();
        obstacleBlocktopdown.avatarcollide();
        obstacleBlockacross.avatarcollide();
        bounce(); 
    
        }
    
        void run2() {
        obstacleBlocklvl2_uno.display();
        obstacleBlocklvl2_dos.display();
        obstacleBlocklvl2_rocket.display();
        obstacleBlocklvl2_uno.movement();
        obstacleBlocklvl2_dos.movement();
        obstacleBlocklvl2_rocket.movement(); 
        obstacleBlocklvl2_uno.collidelvl2();
        obstacleBlocklvl2_dos.collidelvl2();
        obstacleBlocklvl2_rocket.collidelvl2();
        obstacleBlocklvl2_uno.seekercollidelvl2(); 
        obstacleBlocklvl2_dos.seekercollidelvl2();
        obstacleBlocklvl2_rocket.seekercollidelvl2();
        obstacleBlocklvl2_uno.avatarcollidelvl2();
        obstacleBlocklvl2_dos.avatarcollidelvl2();
        obstacleBlocklvl2_rocket.avatarcollidelvl2();
        obstacleBlocklvl2_uno.bounce2();
        obstacleBlocklvl2_dos.bounce2();
        obstacleBlocklvl2_rocket.bounce2();
        bounce2(); 
    
        }
    
    
       void collide(){
        if (dist(obstacleBlocktopdown.x, obstacleBlocktopdown.y, obstacleBlockacross.x, obstacleBlockacross.y) <= 20){
          speedX = speedX * -1; 
          speedY = speedY * -1; 
                }
       }
    
    
    
       void seekercollide(){
       if (dist(obstacleBlocktopdown.x, obstacleBlocktopdown.y, seekerStone1.x, seekerStone1.y) <= 19 || dist(obstacleBlockacross.x, obstacleBlockacross.y, seekerStone1.x, seekerStone1.y) <= 19){
        background (0); 
        textSize(36); 
        text("You lost the game!", 100, 300);
        seekerStone1.x = 1100;
        seekerStone1.y = 600; 
        playerAvatar.x = 11;
        playerAvatar.y = 11;
        obstacleBlocktopdown.x = width/2; 
        obstacleBlocktopdown.y = 0; 
        obstacleBlockacross.x = 0; 
        obstacleBlockacross.y = height/2;
        playstate = false;
        losestate = true; 
        losscount++; 
        }
      } 
    
    
      void avatarcollide(){
        if (dist(obstacleBlocktopdown.x, obstacleBlocktopdown.y, playerAvatar.x, playerAvatar.y) <= 19 || dist(obstacleBlockacross.x, obstacleBlockacross.y, playerAvatar.x, playerAvatar.y) <= 19){
        seekerStone1.x = 1100;
        seekerStone1.y = 600; 
        playerAvatar.x = 11;
        playerAvatar.y = 11;
        obstacleBlocktopdown.x = width/2; 
        obstacleBlocktopdown.y = 0; 
        obstacleBlockacross.x = 0; 
        obstacleBlockacross.y = height/2;
        playstate = false;
        losestate = true; 
        losscount++;
        }
      }
    
      void seekercollidelvl2(){
         if (dist(obstacleBlocklvl2_uno.x, obstacleBlocklvl2_uno.y, seekerStone2.x, seekerStone2.y) <= 19 || dist(obstacleBlocklvl2_dos.x, obstacleBlocklvl2_dos.y, seekerStone2.x, seekerStone2.y) <= 19 ||
       dist(obstacleBlocklvl2_rocket.x, obstacleBlocklvl2_rocket.y, seekerStone2.x, seekerStone2.y) <= 19){
        playstate2 = false;
        losestate2 = true; 
         background (0); 
        textSize(36); 
        text("You lost the game!", 100, 300);
        seekerStone2.x = 450;
        seekerStone2.y = 930; 
        playerAvatar2.x = 450;
        playerAvatar2.y = 20;
        obstacleBlocklvl2_uno.x = 10;
        obstacleBlocklvl2_uno.y = 10; 
        obstacleBlocklvl2_dos.x = 940; 
        obstacleBlocklvl2_dos.y = 940;
        obstacleBlocklvl2_rocket.x = 2;
        obstacleBlocklvl2_rocket.y = height/3;  
        losscount++; 
        }
    
      }
    
      void avatarcollidelvl2(){
        if (dist(obstacleBlocklvl2_uno.x, obstacleBlocklvl2_uno.y, playerAvatar2.x, playerAvatar2.y) <= 19 || dist(obstacleBlocklvl2_dos.x, obstacleBlocklvl2_dos.y, playerAvatar2.x, playerAvatar2.y) <= 19 ||
       dist(obstacleBlocklvl2_rocket.x, obstacleBlocklvl2_rocket.y, playerAvatar2.x, playerAvatar2.y) <= 19){
         background (0); 
        textSize(36); 
        text("You lost the game!", 100, 300);
        seekerStone2.x = 450;
        seekerStone2.y = 930; 
        playerAvatar2.x = 450;
        playerAvatar2.y = 20;
        obstacleBlocklvl2_uno.x = 10;
        obstacleBlocklvl2_uno.y = 10; 
        obstacleBlocklvl2_dos.x = 940; 
        obstacleBlocklvl2_dos.y = 940;
        obstacleBlocklvl2_rocket.x = 2;
        obstacleBlocklvl2_rocket.y = height/3; 
        playstate2 = false;
        losestate2 = true; 
        losscount++;
        }
      }
    
         void collidelvl2(){
         if (dist(obstacleBlocklvl2_uno.x, obstacleBlocklvl2_uno.y, obstacleBlocklvl2_dos.x, obstacleBlocklvl2_dos.y) <= 20){
          speedX = speedX * -1; 
          speedY = speedY * -1; 
                }else if(dist(obstacleBlocklvl2_uno.x, obstacleBlocklvl2_uno.y, obstacleBlocklvl2_rocket.x, obstacleBlocklvl2_rocket.y) <= 20) {
                speedX = speedX * -1; 
                 speedY = speedY * -1;   
                }else if(dist(obstacleBlocklvl2_dos.x, obstacleBlocklvl2_dos.y, obstacleBlocklvl2_rocket.x, obstacleBlocklvl2_rocket.y) <= 20){
                 speedX = speedX * -1; 
                 speedY = speedY * -1;     
                }
    
       }
    
    }
    
  • Last of the Code.

      class GoalState{
      float x; 
      float y; 
      float a;
      float b; 
      static final color goalColor = #FFFFFF, outline = #76719B;
      static final int weight = 10; 
     //colors = white, 118, 113, 155; 
    
    
       GoalState() {
    
       }
    
       GoalState(float _x, float _y) {
        x = _x;
        y = _y;  
      }
    
      //Functions 
      void display () {
        //goalColor = (118, 113, 155); 
        strokeWeight(weight); 
        stroke(outline); 
        fill(goalColor);
        point(x, y);
        //strokeWeight(5); 
    
      }
    
      void goalcheck() {
        if (dist(goal.x, goal.y, seekerStone1.x, seekerStone1.y) < 9) {
        playstate = false;
        winstate = true;  
        wincount++;
         } 
    
      }
    
      void goalcheck2() {
        if (dist(goal2.x, goal2.y, seekerStone2.x, seekerStone2.y) < 9) {
        playstate2 = false;
        winstate2 = true;  
        wincount++;
         } 
    
      } 
    }
    
    
    class Seeker {
      // Global Variables 
      float synch_algo; 
      float x = 0;
      float y = 0;
      float c = 0;
      float b = 0;
      static final color seekerColor = #3DA10F, outline = 0; 
    
      // constructors 
      Seeker() {
      }
      Seeker(float _x, float _y, float _a, float _b) {
        x = _x;
        y = _y; 
        c = _a;
        b = _b; 
      }
    
      //Functions 
      void display () {
        //rgb 67, 161, 15;
        stroke(outline); 
        fill(seekerColor);
        ellipse(x, y, c, b);
      }
    
      void constrainToScreen() {
      x = constrain(x, c/2.0, width  - c/2.0);
      y = constrain(y, b/2.0, height - b/2.0);
    }
    
      void synch2avatar(){
        if(playerAvatar.x + 50 > width || playerAvatar.x + 50 < 0){
           seekerStone1.x =  seekerStone1.x;
        }else {
            seekerStone1.x = playerAvatar.y + 50;
        }
        if(playerAvatar.y + 100 > height || playerAvatar.y + 100 < 0){
           seekerStone1.y =  seekerStone1.y;
        }else{
        seekerStone1.y = playerAvatar.x + 100; 
        }
        seekerStone1.display(); 
      }
    
    
    void synch2avatar2(){
      if(950 - playerAvatar2.x  > width || 950 - playerAvatar2.x  < 0){
           seekerStone2.x =  seekerStone2.x;
        }else {
            seekerStone2.x = 950 - playerAvatar2.x ;
        }
        if(950 - playerAvatar2.y  > height || 950 - playerAvatar2.y  < 0){
           seekerStone2.y =  seekerStone2.y;
        }else{
        seekerStone2.y = 950 - playerAvatar2.y; 
        }
        seekerStone2.display(); 
      } 
    }
    
  • I haven't run the code but can see a large number of Boolean variables intended to track state and lots of if conditions testing against these. The problem with that is unless you're very careful multiple conditions can be true at the same time, leading to the unexpected results you describe.

    If only one state should be available at any one time you would have to reset the value of all other states to false when you set another to true. That's really inefficient. Instead have a single int variable and change it's value and test against this in draw. You can use named constants to make the code more readable...

Sign In or Register to comment.