-1

When I run the following code, for some reason it executes as if the else is always true, however when I take away the else statement, the if statements work fine, thus indicating that the if statements do work, I think, however I cannot set Player.moving = true without some kind of else.

if (Player.x > j * 25 - 15 &&
    Player.x < j * 25 + 10 &&
    Player.y > i * 25 &&
    Player.y < i * 25 + 25 &&
    Player.direction === 1
) {
    Player.moving = false;
}
else if (Player.x > j * 25 &&
         Player.x < j * 25 + 25 &&
         Player.y > i * 25 - 15 &&
         Player.y < i * 25 + 10 &&
         Player.direction === 2
) {
    Player.moving = false;
}
else if (Player.x > j * 25 &&
         Player.x < j * 25 + 40 &&
         Player.y > i * 25 &&
         Player.y < i * 25 + 25 &&
         Player.direction === 3
) {
    Player.moving = false;
}
else if (Player.x > j * 25 &&
         Player.x < j * 25 + 25 &&
         Player.y > i * 25 &&
         Player.y < i * 25 + 50 &&
         Player.direction === 4
) {
    Player.moving = false;
}
else {
    Player.moving = true;
}

Where i and j are the indices of the object in a tile map, such that the tilemap looks something like this: var tilemap = [" "," "];

For instance when

if (Player.x > j * 25 &&
         Player.x < j * 25 + 40 &&
         Player.y > i * 25 &&
         Player.y < i * 25 + 25 &&
         Player.direction === 3
) {
    Player.moving = false;
}

is the only code present. The player will cease its movement to the left when the object at [i][j] is to the left of it as expected. However, with the else statement present, it simply continues to move, Player.moving stays true. Thanks for any help!

Fanghole
  • 33
  • 4
  • 1
    i think no other condition are met so it is ending at else statement – Samundra Khatri Oct 18 '15 at 20:04
  • 2
    Please provide enough code in the question itself to replicate this issue. We have no way to evaluate your conditions. Links to external sites rot and we shouldn't have to go off site to see why your code isn't working. questions should be self contained – charlietfl Oct 18 '15 at 20:05

1 Answers1

1

Your if clause was not defined correctly, x can't be both greater than and smaller than a value at the same time. So does y.

You may want something like this :

If ( (Player.x > I || Player.x < J) &&
     (Player.y > M || Player.y < N) &&
      Player.direction === 1
) {...} ...
Chris Wijaya
  • 1,276
  • 3
  • 16
  • 34