Animation With Variables Example
// Variables
 
/*
// These are some examples of variables
int number = -36;
float temperature = 15.425;
char pressedKey = 'q';
boolean understood = true;
*/
 
// in this example we move an ellipse across the screen
 
// For the position of the ellipse we need 2 variables, one for the x and one for the y
int xPosition;
int yPosition;
 
void setup()
{
  size(200,200);
  smooth();
 
  // We begin with the ellipse in the middle of the screen
  xPosition = width/2;
  yPosition = height/2;
}
 
void draw()
{
  // draw the background
  background(100);
  // draw the ellipse
  ellipse(xPosition, yPosition, 20, 20);
 
  // each frame, move the ellipse a little bit more towards the top-right corner
  xPosition = xPosition + 1;
  yPosition = yPosition - 1;
}
 
void mousePressed() {
  // If the mouse is pressed, the ellipse starts back at the center of the screen
  xPosition = width/2;
  yPosition = height/2;
}
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License