how to fix the blinking square problem

Jeroo j;

public class Jeroo {
  int r, c, numFlowers, d;
  //dir 1=w 2=a 3=s 4=d
  boolean isMoving;
  final int w=40;
  public Jeroo(int row, int col, int numFlows) {
    r=row;
    c=col;
    numFlowers=numFlows;
    d=1;
  }
  void hop() { //moves based on direction
    if (d==1)
      r=r+1;
    else if (d==2)
      c=c-1;
    else if (d==3)
      r=r-1;
    else if (d==4)
      c=c+1;
    if (d>4||d<=0)
      d=1;
    println(r/w+", "+c/w+" direction is "+d);
  }
  void hop(int numSpaces) { //hops the num spaces based on dir
    for (int i=0; i<numSpaces; i++) {
      hop();
      println(i);
    }
  }
  int getR() {
    return r;
  }
  int getC() {
    return c;
  }
  int getD() {
    return d;
  }
  void setR(int row) {
    r=row;
  }
  void setC(int col) {
    c=col;
  }
  void turn(int dir) {
    d=dir;
    if (d>4||d<=0)
      d=1;
    println(r/w+", "+c/w+" direction is "+d);
  }
  boolean isFacing(int compdir) {
    if (d==compdir) {
      return true;
    } else {
      return false;
    }
  }
  boolean hasFlower() {
    if (numFlowers>0) {
      return true;
    } else {
      return false;
    }
  }
  void plant() {
    if (hasFlower()) {
      //Flower f=new Flower(r,c);
    }
  }
  void toss() {
    if (hasFlower()) {
      numFlowers--;
    }
    println(numFlowers);
  }
  //void pick(){}
  void display() {
    //triangle(width/2,0,0,height/2,width,height/2);
    fill(255-60);
    stroke(0);
    for (int i=0; i<width; i+=w) {
      for (int j=0; j<height; j+=w) {
        rect(i, j, w, w);
      }
    }

    fill(#56FF15);
    //triangle(r  +r/2,c,r-r,c,r,c);
    rect(r*w+5, c*w+5, w-10, w-10);
    /*fill(#FF0D00);
     textAlign(CENTER);
     textSize(50);
     text("Jeroo by Gantz", width/2, height/2);*/
  }
}
void setup() { //insert all move commands here
  frameRate(5);
  j=new Jeroo(0, 0, 0);
  size(600, 600);
  println(j.isFacing(1));////
  j.turn(4);
  println(j.hasFlower());
}
void draw() { //draw graph and the jeroo
  j.hop();
  j.display();
  j.hop();
  j.display();
  j.hop();
  j.display();
  j.hop();
  j.display();
  j.hop();
  j.display();
  j.hop();
  j.display();
  j.hop();
  j.display();
  j.hop();
  j.display();
  j.hop();
  j.display();
  j.hop();
  j.display();
  j.hop();
  j.display();
  j.hop();
  j.display();
  j.hop();
  j.display();
  j.hop();
  j.display();
  j.hop();
  //j.turn(j.d+1);
}

as you can see, the square isn't displaying constantly and doing an odd blinking thing.

Answers

  • edited October 2016

    Hi. As I do not have Processing installed here, I ported your code to pure Java, so I could run it on Eclipse IDE.

    I got no troubles here, the squares seem like a static picture, no blinking (I understood that, in order to reproduce the error, they were supposed to blink).

    So, if my porting did not broke (or corrected) your example (against my will), my guess would be a performance problem (although your code is not very heavy). Have you tried removing the "println" calls?

    Good luck.

  • edited October 2016 Answer ✓

    @lolnyancats --

    You need to understand how draw() works.

    draw() is called many times a second. You can't just add a long line of function calls (hop, turn) and expect them to all slowly play out like an animation. All of those hops and turns will be executed, then the screen will be drawn once, then all will happen again -- 60 times a second -- or whatever frameRate is set to, in your case 5 -- and then the screen will be updated once, and then all the hops and turns will immediately be executed again, etc. etc.

    Try this version of your sketch. I moved the setup() and draw() to the top of the code, and replaced the long set of instructions in draw with a few commands in keyPressed() to allow keyboard control (and a bit of drawing on the square to show which direction the square is facing).

    /**
     *  forum.processing.org/two/discussion/18697/how-to-fix-the-blinking-square-problem
     * 2016-10-24
     **/
    Jeroo j;
    
    void setup() { //insert all move commands here
      frameRate(5);
      j=new Jeroo(5, 5, 0);
      size(600, 600);
      println(j.isFacing(1));////
      println(j.hasFlower());
    }
    
    void draw() { //draw graph and the jeroo
      j.display();
    }
    
    void keyPressed(){
      if(keyCode == LEFT) { j.turn(j.d+1); }
      if(keyCode == RIGHT){ j.turn(j.d-1); }
      if(keyCode == UP)   { j.hop(); }
    }
    
    public class Jeroo {
      int r, c, numFlowers, d; //dir 1=w 2=a 3=s 4=d
      boolean isMoving;
      final int w=40;
      public Jeroo(int row, int col, int numFlows) {
        r=row;
        c=col;
        numFlowers=numFlows;
        d=1;
      }
      void hop() { //moves based on direction
        if (d==1) r=r+1;
        else if (d==2) c=c-1;
        else if (d==3) r=r-1;
        else if (d==4) c=c+1;
        this.status();
      }
      void hop(int numSpaces) { //hops the num spaces based on dir
        for (int i=0; i<numSpaces; i++) {
          this.hop();
        }
      }
      int getR() { return r; }
      int getC() { return c; }
      int getD() { return d; }
      void setR(int row) { r=row; }
      void setC(int col) { c=col; }
      void turn(int dir) {
        d=dir;
        if (d>4){ d=1; }
        if (d<1){ d=4; }
        this.status();
      }
      boolean isFacing(int compdir) {
        if (d==compdir) { return true; }
        else { return false; }
      }
      boolean hasFlower() {
        if (numFlowers>0) { return true; }
        else { return false; }
      }
      void plant() {
        if (hasFlower()) { } //Flower f=new Flower(r,c);
      }
      void toss() {
        if (hasFlower()) { numFlowers--; }
      }
      void display() {
        this.grid();
        pushMatrix();
          translate(r*w+w/2.0,c*w+w/2.0);
          rotate(HALF_PI * -(d-1));
          fill(#FFFF00);
          rect(-15, -15, 2*15, 2*15);
          fill(#56FF15);
          triangle(-15, -15, 15, 0, -15, 15);
          fill(0);
          textSize(10);
          textAlign(CENTER, CENTER);
          text(d, 0, 0);
        popMatrix();
      }
      void grid() {
        fill(255-60);
        stroke(0);
        for (int i=0; i<width; i+=w) {
          for (int j=0; j<height; j+=w) {
            rect(i, j, w, w);
          }
        }
      }
      void status(){
        println("r:", r, "c:", c, "direction:", d);
      }
    } 
    

    JerooSquareGame

Sign In or Register to comment.