04.04.2013 Views

Processing: Creative Coding and Computational Art

Processing: Creative Coding and Computational Art

Processing: Creative Coding and Computational Art

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Animation is a perfect example of this effect. If at least 12 images per second move in<br />

front of your eyes, you begin to see continuous movement. But of course, you are really<br />

only looking at a series of static images.<br />

The frameRate(30) function call at the bottom of the setup() function controls the rate<br />

at which the animation runs. You can pass different values to the frameRate() call as a<br />

single argument between the parentheses to change the speed of the animation. If you<br />

don’t include the frameRate() call, the default speed is 60 frames per second. I suggest<br />

trying some different values in the call to see how it affects the speed <strong>and</strong> smoothness of<br />

your animation. The draw() function below setup() controls animation in <strong>Processing</strong>.<br />

Simply by including it, an animation loop is created in your sketch, which I’ll cover in a lot<br />

more detail later.<br />

In the draw() function, I update the background color, which helps create the illusion of<br />

the bouncing ball. Try commenting out the background(0) line by putting two forward<br />

slashes (//) in front of the comm<strong>and</strong>, <strong>and</strong> watch what happens. The effect is actually<br />

pretty cool, <strong>and</strong> what you would do if you wanted the background to be incrementally<br />

painted, rather than entirely updated each loop iteration.<br />

Next I call the ellipse function, ellipse(xpos, ypos, wdth, ht);, passing in the four variables<br />

for the ball’s x position, y position, width, <strong>and</strong> height. Then I increment the xpos <strong>and</strong><br />

ypos variables using the assignment shortcut operators. This gets the ball moving.<br />

However, the ball will never stop, so I need to do some thinking for the computer <strong>and</strong> set<br />

up some conditions for when the ball should deflect or bounce off the frame.<br />

The two conditional statements are essentially identical, except of course one controls the<br />

x movement <strong>and</strong> the other the y movement. So what I describe following for x also applies<br />

to y.<br />

I begin the conditional with if. if is a reserved word in Java <strong>and</strong> <strong>Processing</strong> that expects a<br />

condition to follow it surrounded by parentheses. Conditional statements rely on Boolean<br />

logic (true or false) to decide how to respond. The default test is for truth. So when I write<br />

if(someStatement == true), I could also just write if (someStatement), which automatically<br />

checks for truth. If the conditional statement is true, program execution occurs on<br />

the next line following the test statement. For example:<br />

if (5>4)<br />

print("hello");<br />

The program should output hello. Although you can write your statement the way I did<br />

previously without curly braces, when there is only one line of code after the if statement,<br />

I don’t recommend it. Instead, I recommend always writing it like this:<br />

if (5>4) {<br />

print("hello");<br />

}<br />

The curly braces make it easier to read your conditional statements, <strong>and</strong> as you begin to<br />

write more complex statements, you’ll need the curly braces anyway. If you do not use the<br />

curly braces, then only the next line of code will be executed if the condition is true.<br />

CODE GRAMMAR 101<br />

79<br />

3

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!