6

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!

Rick Royd Aban
  • 904
  • 6
  • 33

2 Answers2

5

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;
   }
IAmGroot
  • 13,760
  • 18
  • 84
  • 154
  • That is one of my problem. TouchX and StanX have never been equal. I tried viewing the logcat for debugging but it shows an interval of 7 instead. Like: StanX: 24 TouchX: 30 StanX: 31 TouchX: 30 StanX: 38 TouchX: 30 – Rick Royd Aban Jan 27 '13 at 10:13
  • @NobodyWantsTo So you are increasing StanX in multiples of something rather than 1 at a time. In that case, change the if statements to check a range. See modified code. – IAmGroot Jan 27 '13 at 10:31
  • Woops, got it wrong way around.. see now. Basically check its within 8 (or what ever value) of touchX. rather than exactly equals – IAmGroot Jan 27 '13 at 10:35
  • Thanks Doomsknight! Just wondering how did andengine manage stanley`s walking that it has to have an interval of seven? Anyway, player class is an animated sprite and player instance has a dimension of 240X165 – Rick Royd Aban Jan 29 '13 at 15:23
  • Im not sure. It might be customizable or an accessable value. Consider hitting the tick as it solved your problem(?) :) Ive not personally used andengine. It might change depending on screen density.. something to look into. – IAmGroot Jan 29 '13 at 15:29
0

try this one

setOnSceneTouchListener(new IOnSceneTouchListener() {
@Override
    public boolean onSceneTouchEvent(Scene scene, TouchEvent event) {
    int touchX = (int) (event.getX() - (sCHARStanley.getWidth() / 2)); 

    //so that your sprite will go to the touched part of the screen
}