09.04.2016 Views

www.ebook777.com

Make_Getting_Started_with_Processing_Second_Edition

Make_Getting_Started_with_Processing_Second_Edition

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

Example 5-10: Click the Mouse<br />

The mousePressed variable is used along with the if statement to<br />

determine when a line of code will run and when it won’t. Try this<br />

example before we explain further:<br />

void setup() {<br />

size(240, 120);<br />

strokeWeight(30);<br />

}<br />

void draw() {<br />

background(204);<br />

stroke(102);<br />

line(40, 0, 70, height);<br />

if (mousePressed == true) {<br />

stroke(0);<br />

}<br />

line(0, 70, width, 50);<br />

}<br />

In this program, the code inside the if block runs only when a<br />

mouse button is pressed. When a button is not pressed, this<br />

code is ignored. Like the for loop discussed in “Repetition” on<br />

page 40, the if also has a test that is evaluated to true or false:<br />

if (test) {<br />

statements<br />

}<br />

When the test is true, the code inside the block is run; when the<br />

test is false, the code inside the block is not run. The computer<br />

determines whether the test is true or false by evaluating the<br />

expression inside the parentheses. (If you’d like to refresh your<br />

memory, the discussion of relational expressions is in Example<br />

4-6 on page 41.)<br />

Response 57

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

Saved successfully!

Ooh no, something went wrong!