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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

72 Getting Started with Processing<br />

<strong>www</strong>.<strong>ebook777.com</strong><br />

}<br />

float mx = map(mouseX, 0, width, 60, 180);<br />

line(mx, 0, mx, height); // Black line<br />

The map() function makes the code easy to read, because the<br />

minimum and maximum values are clearly written as the<br />

parameters. In this example, mouseX values between 0 and width<br />

are converted to a number from 60 (when mouseX is 0) up to 180<br />

(when mouseX is width). You’ll find the useful map() function in<br />

many examples throughout this book.<br />

Robot 3: Response<br />

This program uses the variables introduced in Robot 2 (see<br />

“Robot 2: Variables” on page 47) and makes it possible to<br />

change them while the program runs so that the shapes<br />

respond to the mouse. The code inside the draw() block runs<br />

many times each second. At each frame, the variables defined<br />

in the program change in response to the mouseX and mouse<br />

Pressed variables.<br />

The mouseX value controls the position of the robot with an easing<br />

technique so that movements are less instantaneous and<br />

feel more natural. When a mouse button is pressed, the values<br />

of neckHeight and bodyHeight change to make the robot short:

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

Saved successfully!

Ooh no, something went wrong!