Maintaining Synchronization

I'm trying to maintain the synchronization between the Avatar object and Seeker object. The seeker moves depending on the Avatar's movements and I need the seeker to stop when it hits the sides of the screen and then maintain it's intended distance/synchronization when it moves away from the wall. For example, if the x,y coordinates of the Seeker are the x+5, y+3 of the avatar's coordinates, then I want it to maintain that formula at all times. If it hits a wall, it's supposed to stop. But once it moves (when the avatar objects moves in such a way to allow it to move off the wall) it's supposed to maintain that same seeker (x,y) = avatar (X+5,y+3) synchronization. I can't get the code to do that here so I'm asking for some help. Here is the code.

Blocker obstacleBlocktopdown; 
Blocker obstacleBlockacross;
Blocker obstacleBlockrocket;
Avatar playerAvatar;
Seeker seekerStone1; 
GoalState goal; 
PImage titlecard; 



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



void setup(){
  size(950, 950); 
  obstacleBlocktopdown = new Blocker(width/2, 1, 20, 20, 0, 1.2); 
  obstacleBlockacross = new Blocker(1, height/2, 20, 20, 2, .4);
  obstacleBlockrocket = new Blocker ( 2, height/3, 15, 15, 2, 2.5); 
  playerAvatar = new Avatar(11, 11, 18, 18);  
  seekerStone1 = new Seeker(1100, 600, 18, 18);  
  goal = new GoalState(790, 500);
  titlecard = loadImage("BluePulsarWave.jpg"); 
  //winner = new SoundFile(this, "Marvelous Battle OST's The Intrepid - DASH.mp3"); 

}


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;
    }
    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;  
    }

  }
 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////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;  
   }
  }

 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////PAUSE STATE 
  if (pausestate == true) {
    background(0); 
    playerAvatar.display();
    seekerStone1.display(); 
    obstacleBlocktopdown.display();
    obstacleBlockacross.display();
    obstacleBlockrocket.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;  
    }

  }

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////PLAY STATE  
  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();
  } 


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

     if (winstate) {

       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 restart", 100, 800); 
       //winner.play(); 

       if (key == ' ') {
         winstate = false;
         playstate = true; 
         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;       
       }
      }

  }


  //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 constrainToScreen() {
  x = constrain(x, a/2.0, width  - a/2.0);
  y = constrain(y, b/2.0, height - b/2.0);
}

}



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 run() {
    obstacleBlocktopdown.display();
    obstacleBlockacross.display();
    obstacleBlockrocket.display();
    obstacleBlocktopdown.movement();
    obstacleBlockacross.movement();
    obstacleBlockrocket.movement(); 
    obstacleBlocktopdown.collide();
    obstacleBlockacross.collide();
    obstacleBlocktopdown.seekercollide(); 
    obstacleBlockacross.seekercollide();
    obstacleBlocktopdown.avatarcollide();
    obstacleBlockacross.avatarcollide();
    bounce(); 
    obstacleBlockrocket.bounce(); 

    }


   void collide(){
    if (dist(obstacleBlocktopdown.x, obstacleBlocktopdown.y, obstacleBlockacross.x, obstacleBlockacross.y) <= 20){
      speedX = speedX * -1; 
      speedY = speedY * -1; 
            }else if(dist(obstacleBlocktopdown.x, obstacleBlocktopdown.y, obstacleBlockrocket.x, obstacleBlockrocket.y) <= 20) {
            speedX = speedX * -1; 
             speedY = speedY * -1;   
            }else if(dist(obstacleBlockacross.x, obstacleBlockacross.y, obstacleBlockrocket.x, obstacleBlockrocket.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 ||
   dist(obstacleBlockrocket.x, obstacleBlockrocket.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 || 
    dist(obstacleBlockrocket.x, obstacleBlockrocket.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++;
    }
  }

}



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++;
     } 

  }

}





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(); 
  }
}

Any help will be appreciated.

Answers

  • Duplicate of http://forum.processing.org/two/discussion/10170/maintainig-synchronizations

    Avoid making multiple threads on the same topic, it only induces confusion.

  • I made changes to the code but I'm still having the same problem. Plus that discussion was a month ago. It's a bit obsolete. I don't think anyone would visit that discussion again. Any ideas on how to solve the synching problem?

  • Well, some people visit old topics, and even try to help if unanswered, so the least is to post in the old thread to indicate it is over, and perhaps to link to the new topic to state discussion continues there.

    April 24 - May 9 doesn't like one month for me...

Sign In or Register to comment.