0

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();
    }
}
4

1 回答 1

2

您正在尝试将其switch用作if-else陈述。您不能在开关中使用多个变量,也不能执行除==.

的语法switch是:

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

  case another_expected_value:
    ...
    break;
}

您应该更改您的代码以使用if-else,例如:

if (compX < -2 && compY < -2) {
  ...
} else if (compX > 2 && compY <-2) {
  ...
} else {
  ...
于 2016-05-29T13:05:12.767 回答