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.
IndexSuggestions & BugsWebsite,  Documentation,  Book Bugs › class reference page contains code mistake
Page Index Toggle Pages: 1
class reference page contains code mistake (Read 939 times)
class reference page contains code mistake
Sep 10th, 2008, 12:44pm
 
Below is the code example from the reference page of class with some corrections, explained in the inline comments.

Quote:
// Declare and contruct two objects (h1, h2) from the class HLine
HLine h1 = new HLine(20, 2.0);
HLine h2 = new HLine(50, 2.5);
 
void setup()
{
  size(200, 300);
/*
  The mistake in this example did not stand out
  because of the original 1:1 (200x200px) ratio
  of width/height, therefore I changed it.
*/
  frameRate(30);
}

void draw() {
  background(204);
  h1.update(); 
  h2.update();  

 
class HLine {
  float ypos, speed;
  HLine (float y, float s) {  
    ypos = y; 
    speed = s; 
  } 
  void update() {
    ypos += speed; 
    if (ypos > height) {
/* 
    I replaced "width" with "height", as I think "height" is intended,
    so that the line starts from top again as soon as it hits bottom,
    the mistake just did not stand out,
    because the canvas of the example was quadratic (width=height),
    but the mistake is realized if you change the canvas to a ratio other than 1:1.
*/
      ypos = 0; 
    } 
    line(0, ypos, width, ypos);
  } 
}
Re: class reference page contains code mistake
Reply #1 - Sep 22nd, 2008, 2:46am
 
fixed.
Page Index Toggle Pages: 1