If Else Not Working Properly - Doesn't Work

I’m a complete novice with Processing. I took lessons in Java and C++ years ago and am just now getting back into things so I figured I’d check this out.

I have this code and it seems defective. When I press B, it works fine so far, but when I press A, it seems to go ahead and run the code for pressing B as well despite the fact the number with the variable is set to 1, not 2.

The screen displays the output for pressing A with the output for pressing B directly on top of it. This also happens when pressing any key other than A or B.

Here is the part of the code in question:

void keyPressed() {

  int keyIndex = 3;

  {if (key == 'A' || key == 'a') 

    keyIndex = -1;

  }{ if (key == 'B' || key == 'b') 

    keyIndex = -2;

  }

  {if (keyIndex == -1) 

    // If it's A, go to page x

    background(0);

    textSize(25);

    text("placeholder text",10,30);

    text("press C to...",100,300);

    text("press D to...",100,400);

}

    if (keyIndex == -2) {

      background(0);}

    textSize(25);{

    text("placeholder text",10,30);

    text("press E to...",100,300);

    text("press F to...",100,400);

  } { 

  }

}

Any help is appreciated. I can’t seem to find a lot of good resources on this subject. Please do not suggest I watch some video, I’ve looked everywhere and just can’t figure out what is wrong here. I’m not up for solving some puzzle so I’d like to be told directly what is going on. Thanks!

1 Like

what i noticed first is the program structure by { }
looks confusing.

pls start in a new/other sketch with simple example and DEBUG with print like

void setup() {
  println("not forget to click on draw window first!");
}

void draw() {
}

void keyPressed() {
  if ( key == 'a' ) { 
    println("a");
  }
}

for
if else
see here

1 Like

OK so this does get something in the console, but it’s supposed to go in the draw window. It does seem to do something though so thanks.

How do I get it to go on a draw window now?

Here is what I have…and it gives me that problem where it prints over itself when pressing A.

How do I stop that from happening? Please tell me in plain English here…the tutorials on this site are very hard to follow!

void setup()

{
 size(1000,800); 
 background(0,0,0);
 textSize(25);
 text("Press A or B to choose the next option.",10,30);

}

void draw() { 
  // keep draw() here to continue looping while waiting for keys
}


void keyPressed() {

  {if (key == 'A' || key == 'a') 
    // If it's A, go to page x
    background(0);
    textSize(25);
    text("placeholder text",10,30);
    text("press C to...",100,300);
    text("press D to...",100,400);

  }
  if (key == 'B' || key == 'b') 
    // If it's A, go to page x
    background(0);
    textSize(25);
    text("placeholder text",10,30);
    text("press E to...",100,300);
    text("press F to...",100,400);

  }
{}
1 Like

Please format your code :blush:

It consist on these two steps:

  1. In your code editor (PDE, VS code, Eclipse, etc) ensure you execute the beautifier function. This function automatically indents your code. Auto-indenting makes your code easier to read and helps catching bugs due to mismatch parenthesis, for instance. In the PDE, you use the key combination: ctrl+t
  2. You copy and paste your code in the forum. Then you select the code and you hit the formatting button aka. the button with this symbol: </>

That’s it! Please notice you do not create a new post in case you need to format something you already posted. You can edit your post, copy the code to the PDE, indent the code properly there and then past it back here, format the code and >> save << the edits.

Extra info:

Formatting your code makes everybody’s life easier, your code looks much better plus it ensures your code integrity is not affected by the forum’s formatting (Do you know the forum processes markup code?) Please visit the sticky posts or the FAQ section/post to learn about this, other advantages and super powers you can get in this brand new forum.

Kf

1 Like

Our first concern is that you are using curly brackets in the wrong place.

if( condition ) { // <-- THIS ONE RIGHT HERE!
  // Actions when condition is true.
} else {
  // Actions when condition is false.
}

Notice that the first curly brace comes at the END of the if statement - not BEFORE it!


Your next problem is that you are trying to have states without properly doing states. This is difficult to explain, so instead notice that, right now, you are trying to do drawing inside keyPressed() - don’t. Do all your drawing inside draw().

Example:


int state;

void setup(){
  size(400,400);
  state = 0;
}

void draw(){
  switch(state){
    case 0:
      background(0); // black
      break;
    case 1:
      background(255,0,0); // red
      break;
    case 2:
      background(0,0,255); // blue
      break;
    case 3:
      background(255,0,255); // purple
      break;
  }
}

void mousePressed(){
  state = state + 1;
  if( state == 4 ){
    state = 0;
  }
}

Notice that in this example, we are using the state variable to track when state the sketch is in. clicking changes this, and drawing depends on this value.

You need to change your sketch to do something similar. Have global variables that determine what is drawn. In draw(), use the values of those variables to know what to draw. Change the value in mousePressed() when the user clicks.

3 Likes

your action on keyPressed is:
+1+ background // that cleans the screen
+2+ new text at 10,30 // that overwrites the old text position

so it is your concept to overwrite? or not?

and check again on the
if ( ) { }
following is very wrong, test it for understanding

if (false) 
println("1");
println("2");
println("3");
println("4");

shows


2
3
4