How to order these if? they told me it's messy

-2
$(document).keypress(function( event ) {

    if(mover)
    {

        switch(event.which)
        {   


            case 119: // W
                if(checkearBloque(posX,posY+1))
                    if(checkearnota(posX,posY+1))
                        if(checkeaArbol(posX,posY+1))
                            if(checkeaPoso(posX,posY+1))
                                if(checkeacasa(posX,posY+1))
                                if(checkeaLago(posX,posY+1))
                                    if(checkearmaria(posX,posY+1))
                                        if(checkearPedro(posX,posY+1))
                                            if(checkearLetrero(posX,posY+1)){{{{ {{{{ {
                    posY++;
                    $("#datos").text("arriba");
                    $( "#pj" ).animate({ "top": "-=20px" }, "fast", function(){
                        mover=true;
                    } );
                }}}} }}}} }


            break;
    
asked by Cristopher 15.11.2018 в 01:52
source

3 answers

2

Since you always compare the same values: posX and posY+1 , you can have a function with an object that stores pairs of values. You pass posX and posY+1 and the function will find if there are matches, returning true , or if there are no matches, returning false .

In case of necessity, the code could inform in which element there were coincidences (see message of console.log .

The check would be done with a call like that simply:

if (checkAll(posX,posY+1)){
    posY++;
    $("#datos").text("arriba");
    $( "#pj" ).animate({ "top": "-=20px" }, "fast", function(){
        mover=true;
    } );
}

Here is a code with several example calls.

function checkAll(posX, posY) {
  /*
    *Este objeto representa el estado de cada elemento
    *no conozco tu contexto, pero no será difícil construir
    *algo así, incluso de forma dinámica si fuese necesario
  */
  var myObject = {
    'bloque': [1, 2],
    'nota': [3, 4],
    'arbol': [5, 3],
    'poso': [4, -1],
    'casa': [3, 3],
    'lago': [0, 0],
    'maria': [4, 8],
    'pedro': [9, 9],
    'letrero': [-3, -5]
  };
  
  /*Variable para determinar si hubo coincidencias*/
  var isEqual = false;
  for (var k in myObject) {
    var itemPair = myObject[k];
    if (itemPair[0] === posX && itemPair[1] === posY) {
      console.log('Se encontró coincidencia en ${k}');
      isEqual = true;
      break;
    }
  }
  console.log(isEqual);
  return isEqual;
}

/*Tres ejemplos*/
checkAll(1,2);
checkAll(-3,-5);
checkAll(0,1);
    
answered by 15.11.2018 в 04:07
1

You could create another function

function chequearTodo(posX, posY) {
    return (checkearBloque(posX,posY)
         && checkearnota(posX,posY)
         && checkeaArbol(posX,posY)
         && checkeaPoso(posX,posY)
         && checkeacasa(posX,posY)
         && checkeaLago(posX,posY)
         && checkearmaria(posX,posY)
         && checkearPedro(posX,posY)
         && checkearLetrero(posX,posY));
}

which would allow you to make the switch more readable:

switch(event.which)
{   
    case 119:
        if (chequearTodo(posX, posY + 1) {
        // ....
        }
    break;

    case ...:
        if (chequearTodo(posX + 1, posY) {
        // ....
        }
    break;

    // el resto de casos
}
    
answered by 15.11.2018 в 02:16
0

For what I see you are doing a little game, and what you want to do is check if you can move to that side, well, what is usually done is to create an object that has the property hardness, and from which others inherit objects (in your case block, note, tree, well ...), and what you do is that if the coordinates to which you are going are occupied by an object with hardness to true you deny the movement and if the hardness is false you allow it (The doll can be moved on top of that object).

    
answered by 15.11.2018 в 10:25