We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
Fucntion input (Read 438 times)
Fucntion input
Apr 26th, 2010, 2:31am
 
This one has me scratching my head.... I can use the code below like I want, it counts from one number towards another.

How can I place this code into a single function????
I would need to pass the result of the function back to the function....... I feel like my answer is recursion. I just need a nudge in the right direction. I know that what I want is a Python generator but I don't think Java has anything similar.

void setup(){
 size(200,200);
 background(255);
 frameRate(10);
}

int x=100;
void draw(){
 x=towards(x,10);
 println(x);
}

int towards(int current ,int target){
 if(current!=target){
   if (current <target){
     current=current+1;
   }
   else {
     current=current-1;
   }
 }
 return current;
}
Re: Fucntion input
Reply #1 - Apr 26th, 2010, 3:01am
 
Quote:
class moveToward{
  int current;
  int goal;
  moveToward(int _current, int _goal ){
    current = _current;
    goal = _goal;
  }
  void update(){
    if(current!=goal){
      if (current < goal){
        current++;
      } else {
        current--;
      }
    }
  }
  int getGoal(){ return goal; }
  int getCurrent(){ return current; }
  void setGoal( int _goal ){ goal = _goal; }
  void setCurrent( int _current ){ current = _current; }
}

moveToward mT1;

void setup(){
 size(200,200);
 background(255);
 mT1 = new moveToward( 100, 10 );
 frameRate(10);
}

void draw(){
 mT1.update();
 println( mT1.getCurrent() );
}

Page Index Toggle Pages: 1