how to you make an object stop after a certain amount of clicks.

hi, I'm very new to processing and I've created a program in which the ellipse will move left and right when pressing the arrow keys but I'm trying to stop the ellipse from moving when the door is closed. if the door is open I want it to move in but stop once it reaches the inside of the door. I also want to make sure that it doesn't move off the screen when moving it back right. I'm struggling with the boolean expression (if that even is what I'm supposed to be using) and would really love some help.

this is the code.

void setup() 
{
  size(512, 348);
}

int xPosition = 30;
int yPosition = 280; 



void draw() 
  //keep the image here to continue looping while awaiting the keyboard functions 
{ 
  background(190) ;
  ellipse(xPosition, yPosition, 30, 30);
  // triangle will make up the trees on the right bottom of the sreen. 
  fill(190);


  rect(330, 220, 90, 115); 
  fill(0);
  rect(360, 238, 20, 20);
  ellipse(350, 280, 10, 10);
  strokeWeight(10);
}




// the movement of Mr. Game and Watch from left to right. 
void keyPressed() 
{

  if (keyCode == RIGHT)
    xPosition = xPosition + 60;
  if (keyCode == LEFT) 
    xPosition = xPosition - 60;
}

Answers

  • Please edit your post above by clicking the gear icon > edit.

    Then format your code so people can read it and cut-paste to test -- as is, it is broken.

    Format by highlighting and pressing Ctrl-o to indent. For details, see:

    https://forum.processing.org/two/discussion/15473/readme-how-to-format-code-and-text

  • edited March 2018 Answer ✓

    In the function keyPressed() at its end you want

    if(xPosition>=width-10)
           xPosition = width-10;  // stop at right wall 
    

    Same principle for left side

    Door

    Not sure where your door is: I guess at 330

    have a global Variable boolean doorIsOpen=true; before setup()

    • change it with a Counter or two counters

    Now when the door is closed player stops, otherwise he moves through the door

    when the door is in the middle of the screen: it’s a little different when player comes from left or comes from right side :

    • But when the door is open (if(doorIsOpen==true)...), ignore the door

    • When it’s closed (if(doorIsOpen==false)...) apply the principle like above on the right side of the screen, stopping the player in front of the door

  • Sorry if I caused any confusion but the code is edited now. thank you Chrisir for the assistance.

Sign In or Register to comment.