How to stop an image with &&.

hey guys, I'm probably going to neck myself if this turns out to be easy but this code is frustrating me. I'm trying to make my person stop at the door when the door is closed and go into the house when its open. this is the code I'm using. I would really appreciate it if someone could point out what I'm doing wrong.

boolean doorIsClosed=false;
boolean doorIsOpen=false;

if ((xPosition>=width-85) && (doorIsClosed))
  xPosition = width - 85;
else if ((xPosition >= width-145) && (doorIsOpen))
  xPosition = width -145;  
Tagged:

Answers

  • Having two booleans to describe the same but opposite things defeats the purpose of what a boolean is. In your case, wouldn't be enough to drop the second if and just use the else clause? It is not clear what 145 and 85 are here. Are you arriving from each side of the door?

    Kf

  • Re:

    boolean doorIsClosed=false;
    boolean doorIsOpen=false;
    

    @kfrajer wrote:

    Having two booleans to describe the same but opposite things defeats the purpose of what a boolean is.

    To use a metaphor: This is like installing two light switches to control one light: one switch can only turn the light off, one switch can only turn the light on. Don't do that. One switch is enough -- and better.

    1. You should use provide a runnable piece of code. That way we can actually run the code and see what happens. Plus some piece of the code that you thought was unimportant may in fact be crucial to the problem, so providing a runnable piece of code might uncover that information early.

    2. When sharing your code, it might be a good idea to go through the code and make sure as much as possible of the code explains itself. Either by using comments, or descriptive variable names. "xPosition" tells us we are talking about an x-position, but who's x-position? And what does the + 85/145 mean?

    3. What the others are saying about boolean.

    Now to solving this:

    I adsume you have a moving person and that you have a variable speed/velocity that determines the movement of the person. If the movement speed is constant, you could use a boolean to decide whether or not the person is moving or not. So if it's set to true and it reaches the door, you need to check if the door is open and change the boolean to false if the door is closed.

    If the movement speed is not constant you can just set the velocity or speed to 0. That way the location will not change.

Sign In or Register to comment.