Using a Variable
A variable is a very useful programming tool. A variable is a way to store something, for example, a number in the computer's memory that can then be changed to change the behavior of a program. For example, you might have a variable keep track of the position of an object as it moves across a screen. You might use a variable to keep track of the score of a game, or how many seconds have elapsed.
When using a variable, there are four steps.
THE FOUR STEPS FOR VARIABLES
DECLARE IT - You must first tell the computer what type of variable you want to store and give it a name. For now, declare your variables before setup().
INITIALIZE IT - You must always put an initial value in the variable, otherwise it will just be junk. In setup() is a great place to initialize something.
USE IT - use the variable in a drawing command (for example in point, line, stroke)
CHANGE IT - Change the value of the variable before you loop again. At the bottom of the draw() function is best.
For example here is a program that I wrote to draw smaller and smaller points in the same place.
int dotSize; // DECLARE IT - a variable called dotSize void setup() { size(500,500); background(0); dotSize = 500; //INITIALIZE IT - set dotSize to a starting value } void draw() { strokeWeight(dotSize); // USE IT - use dotSize to change the size of the point stroke(random(255),random(255),random(255)); point(250,250); dotSize = dotSize-5; // CHANGE IT - get the number dotsize, subtract 5 from it and put it back }
Watch this video:
http://funprogramming.org/7-Animate-horizontal-lines-use-a-variable.html [Video 7]
Type in Mr. Fun’s program from the video and run it.
Now change it as Mr. Fun suggests - make vertical lines
If you need help, watch this video: