-1

I created these functions:


function generatePosition(){

    var locX = Math.floor(Math.random() * 12) + 5;
    var locY = Math.floor(Math.random() * 10) + 3; 
    console.log('MOVE 0 ' + locX + ' ' + locY );     // MOVE <pacId> <x> <y>

 }

 function sonoArrivato(){

    var locW = [locX, locY];  //position to go
    var locP = [x, y];    //position my pacman
    var test = 0;

    if(locP != locW){
        test = 0;
    } else{
        test = 1;
    }

 }

 function maintainPosition(){

    var saveX = locX;
    var saveY = locY;

    console.log('MOVE ' + saveX + ' ' + saveY );
 }

And I want that my subject goes to the position created and then create a new one from random function

        generatePosition();
        if (sonoArrivato.test != 0){
            generatePosition();
        }else{
            maintainPosition();
            console.error(visiblePelletCount);
        }

But it's generating a new position every time

Sara Briccoli
  • 141
  • 3
  • 11

1 Answers1

1

sonoArrivato is a function
sonoArrivato.test is undefined
undefined!=0 is true
that's why it always generates a new position

change the function sonoArrivato by adding return test; at the end and change the if statement to if(sonoArrivato()!==0){generatePosition();}else{...}

pank
  • 132
  • 1
  • 3