How to make an "autonomous" function

edited July 2015 in How To...

Hello

I'm having a hard time figuring out the way function are, or should be, triggered. What I mean is , draw() for instance is looping, always, while a function that I'll build will only run once each time I trigger it. But then, if I need to have this function to loop, I'm kind of stuck with triggering it over and over again _from _ draw()

I made an example of, what I would like to do it triggering the autoBall() function 1 time and having it perform it's thing until it is done... _without _ pushing it from draw() (because when the thing I program becomes big, I'd like not to have to go all the way back to draw() to trigger it)

but maybe this example will make my point easier to understand

int startTime;
int stopTime;
float p ;

void setup() {
  size(300, 200);
}

void draw() {
  background(0);
  strokeWeight(5);
  stroke(255, 128, 0);
  noFill();
  line(20, 100, width-20, 100);
  //
  textAlign(CENTER);
  textSize(16);
  text("press spacebar", width/2, 50);
  text(p, width/2, 70);
}

void  keyPressed() {
  if (keyCode == ' ' ) {
    autoBall();
  }
}

void autoBall() {
  startTime = millis();
  stopTime = startTime + 2000;
  while (millis () < stopTime) {
    p = map(millis(), startTime, stopTime, 20, width-20);
    println(p);
    ellipse(p, 100, 20, 20);
  }
}

Answers

  • Draw in itself loops 60 times per second

    To see the ball move, use this feature pls

    So store the mills in keyPressed (startTime and stopTime)

    And in autoBall rather check if time is not up and display ball.....

    Then you'll see the movement

    Call autoBall from draw() and trigger the values from keyPressed

  • To answer your question:

    Actually I think it's common to call stuff from draw() or subroutines of it. That's just ok and the way it is.

  • edited July 2015

    thank you Chrisir

    I don't understand why the for loop is performed 2000 times and but I cannot include the rect() into it... The thing is I use processing from within eclipse. Is there a draw() kind of function yo always have to call everytime from when you use plain java ?

    is there a way to keep track of things that loop (all the way to draw()) ? Is it that way because everything have to keep synchronous ?

    (yeah... I wish I could take a proper coding class some day ;))

  • int startTime;
    int stopTime;
    float p ;
    
    void setup() {
      size(300, 200);
    }
    
    void draw() {
      background(0);
      strokeWeight(5);
      stroke(255, 128, 0);
      noFill();
      line(40, 100, width-40, 100);
      //
      textAlign(CENTER);
      textSize(16);
      text("press spacebar", width/2, 50);
      text(p, width/2, 70);
    
      autoBall();
    }
    
    void  keyPressed(){
      if (keyCode == 32 ) {  //see how 32 is keycode for spacebar
        startTime = millis();
        stopTime = startTime + 1000;
      }
    }
    
    void autoBall() {
      if  (millis () < stopTime) {
        p = map(millis(), startTime, stopTime, 40, width-40);
        println(p);
        //
        ellipse(p, 100, 20, 20);
      }
    }
    

    It works good like this... but in my demo, the autoBall is soooo far from draw :O

  • Answer ✓

    Until you start using threads (an advanced topic) you have to think of it like this: the processor can only follow one instruction at a time. It runs everything in setup() then the draw() loop begins. Each command in draw() is executed in turn till it gets to the end then it starts again. In pure Java if you wanted to do animation you would set up a loop just like draw()...

    You could put all your code in draw(), but that would get really messy, so you create functions and classes to wrap up code that performs a certain task. A function just takes the flow outside of draw() temporarily: everything in your function executes and then you return to the next step in draw(). That's normal and generally desirable when creating animation. You might want to look at the Objects tutorial as that provides a more effective way to externalise stuff from draw and the sketch's scope; but you'll still have to call Class method(s) from draw()...

Sign In or Register to comment.