javascriptcanvasswitch-statementweb-scripting

javascript - switch in a function with 2 variables


I try to make a game but in following code I have defined a function with a for-loop wherein I puted an switch-statement. Now I see with console.log() that all worked fine but the switch-statement is not working, even if compX and compY both are for example above 2, he doesn´t run the code in that case-statement of switch. I have tested that with console.log() where I asked in that case-statement for ´rux´and ´ruy´, but nothing appeared in the console. So has someone an idea what I am doing wrong? and has maybe someone an example how it does work? Thank you all for every tiny answer!

function updatePosition() {
    for (var pc = policeNum; pc > 0; pc--) {
        policeRef.child(pc).once('value', function (snapshot) {
            var oldData = snapshot.val();
            //KI:
            var compX = newX - oldData.X;
            var compY = newY - oldData.Y;
            console.log('We found X:', compX);
            console.log('We found Y:', compY);
            switch (compX, compY) {
            case compX < -2 && compY < -2: //links und oben
                var lox = oldData.X - pixelWidth;
                var loy = oldData.Y - pixelHeight;
                policeRef.child(pc).update({
                    X: lox,
                    Y: loy
                });
                break;
            case compX > 2 && compY < -2: //rechts und oben
                var rox = oldData.X + pixelWidth;
                var roy = oldData.Y - pixelHeight;
                policeRef.child(pc).update({
                    X: rox,
                    Y: roy
                });
                break;
            case compX < -2 && compY > 2: //links und unten
                var lux = oldData.X - pixelWidth;
                var luy = oldData.Y + pixelHeight;
                policeRef.child(pc).update({
                    X: lux,
                    Y: luy
                });
                break;
            case compX > 2 && compY > 2: //rechts und unten
                var rux = oldData.X + pixelWidth;
                var ruy = oldData.Y + pixelHeight;
                console.log('We found rux:', rux);
                console.log('We found ruy:', ruy);
                policeRef.child(pc).update({
                    X: rux,
                    Y: ruy
                });
                break;
            case compX > -2 && compX < 2 && compY > 2: //unten
                var uy = oldData.Y + pixelHeight;
                console.log('We found uy:', uy);
                policeRef.child(pc).update({
                    Y: uy
                });
                break;
            case compX > -2 && compX < 2 && compY < -2: //oben
                var oy = oldData.Y - pixelHeight;
                policeRef.child(pc).update({
                    Y: oy
                });
                break;
            case compY > -2 && compY < 2 && compX > 2: //rechts
                var rx = oldData.X + pixelWidth;
                policeRef.child(pc).update({
                    X: rx
                });
                break;
            case compY > -2 && compY < 2 && compX < 2: //links
                var lx = oldData.X - pixelWidth;
                policeRef.child(pc).update({
                    X: lx
                });
                break;

            }
            context.clearRect(oldData.Y, oldData.X, pixelHeight, pixelWidth)
        });
        updateDraw();
    }
}

Solution

  • You are trying to use switch as if it is an if-else statement. You cannot use multiple variables in a switch, nor can it perform any comparison other than ==.

    The syntax for switch is:

    switch(expression) {
      case expected_value: // i.e, expression == expected_value
        ...
        break;
    
      case another_expected_value:
        ...
        break;
    }
    

    You should change your code to use if-else instead, e.g.:

    if (compX < -2 && compY < -2) {
      ...
    } else if (compX > 2 && compY <-2) {
      ...
    } else {
      ...