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.

162 Getting Started with Processing<br />

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

Robot 9: Arrays<br />

Arrays make it easier for a program to work with many elements.<br />

In this example, an array of Robot objects is declared at<br />

the top. The array is then allocated inside setup(), and each<br />

Robot object is created inside the for loop. In draw(), another<br />

for loop is used to update and display each element of the bots<br />

array.<br />

The for loop and an array make a powerful combination. Notice<br />

the subtle differences between the code for this example and<br />

Robot 8 (see “Robot 8: Objects” on page 147) in contrast to the<br />

extreme changes in the visual result. Once an array is created<br />

and a for loop is put in place, it’s as easy to work with 3 elements<br />

as it is 3,000.<br />

The decision to load the SVG file within setup() rather than in<br />

the Robot class is the major change from Robot 8. This choice<br />

was made so the file is loaded only once, rather than as many<br />

times as there are elements in the array (in this case, 20 times).<br />

This change makes the code start faster because loading a file<br />

takes time, and it uses less memory because the file is stored<br />

once. Each element of the bot array references the same file:

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

Saved successfully!

Ooh no, something went wrong!