Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Character never stops from walking

I am creating a game which uses andendgine and here is my code:

Player stanley = new Player();
...

scene.registerUpdateHandler(new IUpdateHandler() {
  public void onUpdate(float pSecondsElapsed) {

  stanX = stanley.getX();
  destX = x.getX();

  if(destX < stanX){ 
    if(hasMovedRight == 1){
      stanley.stop();
      hasMovedRight = 0;
    }
    else{
      stanley.moveLeft();
      hasMovedRight = 0
      hasMovedLeft = 1;
    }
  }

  if(destX > stanX){   
    if(hasMovedLeft == 1){
       stanley.stop();
       hasMovedLeft == 0;
    }   
    else{
      stanley.moveRight();
      hasMovedLeft = 0;
      hasMovedRight = 1;
    }
  }
}
}

what i want is to stop Player from walking whenever his position X is equal to the touched area X. The problem is it never stop from walking. Thanks!

like image 477
Rick Royd Aban Avatar asked Jan 27 '13 09:01

Rick Royd Aban


1 Answers

Your if statements are missing an element where destX == stanX. and you should really use else if. See modified code below.

  if(destX + 8 < stanX){ 
    if(hasMovedRight == 1){
      stanley.stop();
      hasMovedRight = 0;
    }
    else{
      stanley.moveLeft();
      hasMovedRight = 0
      hasMovedLeft = 1;
    }
  }
  else if(destX - 8 > stanX){   
    if(hasMovedLeft == 1){
       stanley.stop();
       hasMovedLeft == 0;
    }   
    else{
      stanley.moveRight();
      hasMovedLeft = 0;
      hasMovedRight = 1;
    }
  }
  else //makes stanley stop. (calls stop method), if at touched x.
  {
      stanley.stop();
      hasMovedRight = 0;
      hasMovedLeft = 0;
   }
like image 62
IAmGroot Avatar answered Oct 26 '22 15:10

IAmGroot