help with if stat

edited March 2015 in Questions about Code

Hi, I have this code. Basically I want that when the mouseY is < than height/2 my variable will change, and depending on that I will draw a different rect. The thig is that the if statements to deal with myB variable aren't actually doing what I want..they keep changing..so where do I put them? I just want myB = 0 and stays that until mouseY < height/2, then myB = 1 and stays that until the next time mouseY < height/2, then myB = 2 and stays that until mouseY < height/2 and then going back to 0

int myB = 0;

void draw(){
if(myB == 0){
  fill(255,0,0);    
  rect(0,0,width/2,height/2);
}
if(myB == 1){
  fill(0,255,0);
  rect(0,0,width/2,height/2);
}
if(myB == 2){
  fill(0,0,255);
  rect(0,0,width/2,height/2);
}
change();

}

void change(){
if(mouseY < height/2){
  if(myB == 0)
    myB = 1;
 else if(myB == 1)
    myB = 2;
 else if(myB == 2)
    myB = 0;
  }
}
Tagged:

Answers

  • Answer ✓
    int myB = 0;
    boolean seen = false;
    color[] colors = { 
      color(255, 0, 0), color(0, 0, 255), color(0, 0, 255)
    };
    
    void setup() {
      size(220, 220);
    }
    
    void draw() {
      background(0);
      fill(colors[myB]);    
      rect(0, 0, width/2, height/2);
      if (mouseY < height/2) {
        if (!seen) {
          seen = true;
          myB++;
          myB%=3;
        }
      } else {
        seen = false;
      }
    }
    
  • edited March 2015 Answer ✓
    • I believe you're aware that by default draw() is invoked @ 60 FPS.
    • Therefore your myB increases @ 60 FPS as well when mouseY is at the HALF_TOP!
    • A smarter solution is to keep previousHeightState and check whether it has changed.
    • Only then we update next PALETTE color's idx.

    // forum.processing.org/two/discussion/9931/help-with-if-stat
    
    static final color[] PALETTE = {
      #FF0000, #008000, #0000FF
    };
    
    int idx;
    
    final MouseHeight mh = new MouseHeight();
    
    void setup() {
      size(300, 300, JAVA2D);
    
      smooth(4);
      noLoop();
      frameRate(30);
    
      rectMode(CORNER);
      stroke(0);
      strokeWeight(2.0);
    
      background(-1);
    }
    
    void draw() {
      fill(PALETTE[idx]);
      rect(width>>2, 0, width>>1, height>>1);
    }
    
    void mouseMoved() {
      mh.determineCurrentState();
    
      if (mh.hasStateChanged() && mh.saveCurrentState() == MouseHeight.TOP_HALF) {
        idx = (idx + 1) % PALETTE.length;
        redraw();
      }
    }
    
    class MouseHeight {
      static final byte TOP_HALF = 0, BOTTOM_HALF = 1;
    
      byte currentHeightState, previousHeightState;
    
      byte determineCurrentState() {
        return currentHeightState = mouseY < height>>1? TOP_HALF : BOTTOM_HALF;
      }
    
      boolean hasStateChanged() {
        return currentHeightState != previousHeightState;
      }
    
      byte saveCurrentState() {
        return previousHeightState = currentHeightState;
      }
    }
    
  • edited March 2015

    thank you both..yes I know how processing works but every time I have to deal with this kind of code I struggle..thank you again!

  • just to clarify..if I write (inside the draw) this code

    if(something)
      do this
    else
      do that
    

    when it gets executed (60 fps), in every of those 60fps it will check whether something is true or false right? then for example

    frame 1 something = true; => do this frame 2 something = false; => do that

    am I right?

  • good. thank you!

Sign In or Register to comment.