void

edited September 2014 in Questions about Code

Is it possible to have another void that is looping as void draw() ?

Answers

  • What do you mean by "void"? Void is the return type of a method: if it is void, the method does not return anything, it just does what it's programmed to do. Draw is the actual name of the method. It gets called 60 times a second (by default) by the Processing applet. So if you want a method like draw, you need to call it 60 times a second as well. Just make your own:

    void anotherMethod()
    {
       //do stuff
    }
    

    and call it when you want it to run.

    It is possible to have a method in a separate thread. The easiest syntax is this:

    void setup()
    {
       thread("anotherMethodInAnotherThread");
    }
    
    void draw()
    {
    }
    
    void anotherMethodInAnotherThread()
    {
    }
    

    You can use a while statement to loop it:

    void anotherMethodInAnotherThread()
    {
       while(1>0)
       {
          //things in here get executed for all eternity (or until math changes so that 1 < 0)
       }
    }
    

    and if you want to interrupt it with a mouse click:

    boolean threadRunning = true;
    
    void setup()
    {
      thread("anotherMethodInAnotherThread");
    }
    
    void draw()
    {
    }
    
    void anotherMethodInAnotherThread()
    {
      while(threadRunning)
      {
        //stuff
      }
    }
    
    void mousePressed()
    {
      threadRunning = false;
    }
    
  • edited September 2014

    Only if you register draw() into another public class via undocumented registerMethod():

    // forum.processing.org/two/discussion/7330/void
    
    void setup() {
      size(300, 150, JAVA2D);
      frameRate(1);
      smooth(4);
      fill(-1);
    
      textAlign(CENTER, CENTER);
      textSize(050);
    
      new Drawing();
    }
    
    void draw() {
      background((color) random(#000000));
    }
    
    public class Drawing {
      Drawing() {
        registerMethod("draw", this);
      }
    
      void draw() {
        text("Frames: #" + frameCount, width>>1, height>>1);
      }
    }
    
  • edited September 2014

    i want to make a text "WARNING" tht flashes: ON, wait 1 second, OFF, wait 1 second and on again, and repet that... this was what i made but is not working i dont know why...

    void error()
    {
      toggle = 1;
      delay(1000);
      toggle = 0;
      delay(800);
      error();
    }
    

    in void draw() i have if toggle == 1 it will show the "WARNING" text and if roggle == 0 it wont...

  • edited September 2014 Answer ✓

    Don't use delay(), it stops everything. You can do the same thing by checking millis().

    Also what you are suggesting, another function that is called 60 frames per second, is not necessary. Just have draw() call your function:

    int timer = 0;
    boolean isOn = true;
    
    void setup() {
      size(200, 200);
    }
    
    void draw() {
      warningFunction();
    }
    
    void warningFunction() {
      if (millis()-1000 > timer) {
        timer = millis();
        isOn = !isOn;
    
        if (isOn) println("On");
        else println("Off");
      }
    }
    
  • edited September 2014 Answer ✓

    There are a number of ways why this doesn't work.

    • delay() halts the whole animation thread: if you call it in draw(), it will delay everything that comes after the error() call!
    • calling error() inside error() means you get stuck in an infinite loop. The program will wait for this loop to finish before continuing but since it's infinite, it will never get a chance to continue...

    Instead you need to find a way to swap toggle from 1 to 0 or vice versa every second, without using delay() or recursive calling. Here is what you could use for that:

    • millis(), returns the time from the start of the program in milliseconds
    • frameCount, returns the amount of times draw() was called since the start of the program
    • %, the modulo operator
  • edited September 2014 Answer ✓
    • Never use delay() in Processing, unless within another thread("")!
      Control FPS via frameRate(): http://processing.org/reference/frameRate_.html

    • Don't call a function void. That keyword merely means a function doesn't return anything!

    • You shoulda told us exactly what you were looking for from the beginning!
      Our 1st replies were all useless for ya!

    Anyways, here's what I've come up w/ related to your current request:

    // forum.processing.org/two/discussion/7330/void
    
    static final int FPS = 30;
    
    void setup() {
      size(300, 150, JAVA2D);
      frameRate(FPS);
      smooth(4);
      fill(-1);
    
      textSize(050);
      textAlign(CENTER, CENTER);
    }
    
    void draw() {
      background(0);
    
      int seconds  = frameCount/FPS;
      boolean isOn = (seconds & 1) == 0;
    
      text("WARNING " + (isOn? "ON" : "OFF"), width>>1, height>>1);
      frame.setTitle("Seconds: " + seconds + "\t\tFPS: " + round(frameRate));
    }
    
  • Dear DzAnej,

    solution 1: make a call to current time and check seconds ... for even numbers select color "A" for odd numbers select color "B".

    solution 2: If the frame rate is known, every time the "draw" function is called, you might increment a counter and whenever you like (by comparing the counter) you might change the color and reset the counter

    solution 3: ... and so on...

    Best regards

    Bernd

Sign In or Register to comment.