Example
// Learning Processing
// Daniel Shiffman
// http://www.learningprocessing.com

// Example 7-3: Bouncing ball with functions

// Declare all global variables (stays the same)
int x = 0;
int speed = 1;

// Setup does not change
void setup() {
  size(200,200);
  smooth();
}

void draw() {
  background(255);
  move(); // Instead of writing out all the code about the ball is draw(), we simply call three functions. How do we know the names of these functions? We made them up!
  bounce();
  display();
}

// Where should functions be placed?
// You can define your functions anywhere in the code outside of setup() and draw().
// However, the convention is to place your function definitions below draw().

// A function to move the ball
void move() { 
  // Change the x location by speed
  x = x + speed;
}

// A function to bounce the ball
void bounce() {
  // If we’ve reached an edge, reverse speed
  if ((x > width) || (x < 0)) {
    speed = speed * - 1;
  }
}

// A function to display the ball
void display() {
  stroke(0);
  fill(175);
  ellipse(x,100,32,32);
}
  • http://www.facebook.com/profile.php?id=1234855330 Tanga Lilly

       line(70,200,width, height-i*1);

  • p.graf

    Does calling the function move() multiple times in draw() mess things up, because”globalX” summs up? For every draw(1); I add, the overall speed goes higher …
    void move(int speedFactor) {      x = x  + (speed * speedFactor);   }

    Thanks

  • Alexander

    The additional exercise component asks for replacement code into the void display() function. It reads:

    void display() {
    background(255);
    rectMode(CENTER);
    stroke(0);
    noFill();
    rect(x, y, 32, 32);
    fill(255);
    rect(x-4, y-4, 4, 4);
    rect(x+4, y-4, 4, 4);
    line(x-4, y+4, x+4, y+4);
    }

    Once ready to execute the program, an error occurs that states no “y” has been declared, “The field Component.y is not visible”. There was never a “y” declared in the original coding, therefore it is searching for something that does not exist yet. For this portion, I set, “y = 100″.

    /* This is a repeat of Chapter 2′s Example 5-6: Bouncing Ball
    using functions to simplify coding */

    // Declare global variables (does not change)
    int x = 0;
    int y = 100; // Added in to make program run
    int speed = 2;

    // Setup (does not change)
    void setup() {
    size(200, 200);
    smooth();
    }

    void draw() {
    background(255);
    move();
    bounce();
    display();
    }

    // A function to move the ball
    void move() {
    // Change the X location by speed
    x += speed;
    }

    // A function to bounce the ball
    void bounce() {
    // If we’ve reached an edge, reverse speed
    if ((x > width) || (x < 0)) {
    speed = speed * -1;
    }
    }

    // A function to display the ball
    void display() {
    background(255);
    rectMode(CENTER);
    stroke(0);
    noFill();
    rect(x, y, 32, 32);
    fill(255);
    rect(x-4, y-4, 4, 4);
    rect(x+4, y-4, 4, 4);
    line(x-4, y+4, x+4, y+4);
    }