What bracket is missing on the 6th line!?! (else if (keyCode == DOWN)

    if ( key == CODED) {

        if (keyCode == UP && linkY-20 > 0 && (map.get((int)linkX, (int)linkY-20)== -339816 || map.get((int)linkX, (int)linkY-20)== -340072 || map.get((int)linkX, (int)linkY-20)== -206680 || map.get((int)linkX, (int)linkY-20)== -9145228 || map.get((int)linkX, (int)linkY-20)== -3650548 || map.get((int)linkX, (int)linkY-20)== -16777216 || map.get((int)linkX, (int)linkY-20)== -197380))
          linkY-=5;
          linkImg = linkUp;
        else if (keyCode == DOWN && linkY+20 < height && (map.get((int)linkX, (int)linkY+20)== -339816 || map.get((int)linkX, (int)linkY+20)== -340072 || map.get((int)linkX, (int)linkY+20)== -206680 || map.get((int)linkX, (int)linkY+20)== -9145228 || map.get((int)linkX, (int)linkY+20)== -3650548 || map.get((int)linkX, (int)linkY+20)== -16777216 || map.get((int)linkX, (int)linkY+20)== -197380))
          linkY+=5;
          linkImg = linkDown;
        else if (keyCode == LEFT && linkX-20 > 0 && (map.get((int)linkX-20, (int)linkY)== -339816 || map.get((int)linkX-20, (int)linkY)== -340072 || map.get((int)linkX-20, (int)linkY)== -206680 || map.get((int)linkX-20, (int)linkY)== -9145228 || map.get((int)linkX-20, (int)linkY)== -3650548 || map.get((int)linkX-20, (int)linkY)== -16777216 || map.get((int)linkX-20, (int)linkY)== -197380))
          linkX-=5;
          linkImg = linkLeft;
        else if (keyCode == RIGHT && linkX+20 < width && (map.get((int)linkX+20, (int)linkY)== -339816 || map.get((int)linkX+20, (int)linkY)== -340072 || map.get((int)linkX+20, (int)linkY)== -206680 || map.get((int)linkX+20, (int)linkY)== -9145228 || map.get((int)linkX+20, (int)linkY)== -3650548 || map.get((int)linkX+20, (int)linkY)== -16777216 || map.get((int)linkX+20, (int)linkY)== -197380))
          linkX+=5;
          linkImg = linkRight;
      }
    }

Answers

  • @Nathan101

    Those conditionals are not cool 8-|

    Please put a little bit of extra work and make them more compact and more readable. You have this error and it is not because you are missing a curly bracket. In processing, when you miss something in your code, the compiler will scream at you and it will always tell you a bracket is missing. In your case, you are actually missing a matching parentheses and I believe it is this one:

    if (keyCode == UP && linkY-20 > 0 && ( ...

    for the first line and I will guess it is the same problem for the other conditionals. If you find you are repeating conditions, you can try something like:

    int myAge=66;
    boolean condition1 = myAge >100;  // it stores false
    

    This is what I mean with making them more compact...

    Also keep in mind Processing has a function call map. I personally avoid using processing keywords even though your program will work fine. Maybe try myMap instead.

    Kf

  • I fixed it doing this:

            if (key == CODED) {
    
                if (keyCode == UP && linkY-20 > 0 && (map.get((int)linkX, (int)linkY-20)== -339816 || map.get((int)linkX, (int)linkY-20)== -340072)) {
                  linkImg = linkUp;
                  linkY -= 15;
                } else if (keyCode == DOWN && linkY+20 < height && (map.get((int)linkX, (int)linkY+20)== -339816 || map.get((int)linkX, (int)linkY+20)== -340072)) {
                  linkImg = linkDown;
                  linkY += 15;
                } else if (keyCode == LEFT && linkX-20 > 0 && (map.get((int)linkX-20, (int)linkY)== -339816 || map.get((int)linkX-20, (int)linkY)== -340072)) {
                  linkImg = linkLeft;
                  linkX -= 15;
                } else if (keyCode == RIGHT && linkX+20 < width && (map.get((int)linkX+20, (int)linkY)== -339816 || map.get((int)linkX+20, (int)linkY)== -340072)) {
                  linkImg = linkRight;
                  linkX += 15;
                }
              }
    
  • But, my character won't move now!!!

  • edited March 2017

    also your conditions are

    if (some_very_complicated_condition)
      linkY-=5;
      linkImg = linkUp;
    

    and that just won't work. the the linkY line will run conditionally, the linkImg line will run UNCONDITIONALLY. it actuall means this

    if (some_very_complicated_condition) {
      linkY-=5;
    }
    linkImg = linkUp;
    

    put {}s around blocks of code like this to make sure they both run. in fact, put it around blocks of code even when there is only one line in the block. it's just a good habit to get into.

    actually, that might be the cause of your problem because the else is unexpected.

    if (some_very_complicated_condition) {
      linkY-=5;
    }
    linkImg = linkUp;
    else (another_very_complicated_condition) // what?!
    

    -339816

    what's this? its not clear.

    i would rather split the colour condition from the others and use a local variable:

    if (keyCode == UP && linkY - 20 > 0) {
      pixel = map.get((int)linkX, (int)linkY - 20);
      if (pixel == -339816 || pixel == -340072 || pixel == -206680 || pixel == -9145228 || pixel == -3650548 || pixel == -16777216 || pixel == -197380) {
        linkY -= 5;
        linkImg = linkUp;
      }
    } else ...
    
  • I fixed it doing this:

    ah, ok, good, it needed the brackets. now think about splitting the enormo expression.

  • It says pixel is not a variable

  • And when I make pixel a float, and run it the player still goes right past the walls.

  • Answer ✓

    We would need runnable code to test that bit, what you've posted isn't enough.

    Double check the colours.

  • edited March 2017

    Due to color blending, using get() to determine collision in Processing is very unreliable! :-SS
    Maybe blendMode(REPLACE); can magically fix it. Who knows? :-??

    https://Processing.org/reference/blendMode_.html

Sign In or Register to comment.