06.01.2013 Views

Learning Processing: A Beginner's Guide to Programming Images ...

Learning Processing: A Beginner's Guide to Programming Images ...

Learning Processing: A Beginner's Guide to Programming Images ...

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

194 <strong>Learning</strong> <strong>Processing</strong><br />

Let’s say the result is:<br />

x: 9000000 y: – 900000<br />

x: 9000116 y: – 901843<br />

x: 9000184 y: – 902235<br />

x: 9000299 y: – 903720<br />

x: 9000682 y: – 904903<br />

It is pretty obvious that these values are not reasonable pixel coordinates. So something would be off in<br />

the way the object is calculating its ( x , y ) location. However, if the values were perfectly reasonable, then<br />

you would move on. Maybe the color is the problem?<br />

println( " brightness: " + brightness(thing.col) + " alpha: " + alpha(thing.col));<br />

Resulting in:<br />

brightness: 150.0 alpha: 0.0<br />

Well, if the alpha value of the object’s color is zero, that would explain why you can’t see it! We should<br />

take a moment here <strong>to</strong> remember Tip #3: Simplify. Th is process of printing variable values will be much<br />

more eff ective if we are doing it in a sketch that only deals with the Th ing object. Th is way, we can be sure<br />

that it is not another class which is, say, drawing over the <strong>to</strong>p of the Th ing by accident.<br />

You may have also noticed that the above print statements concatenate actual text with the variables.<br />

(See Chapter 17 for more on concatenation.) It is generally a good idea <strong>to</strong> do this. Th e following line of<br />

code only prints the value of x , with no explanation.<br />

println(x);<br />

Th is can be confusing <strong>to</strong> follow in the message window, especially if you are printing diff erent values in<br />

diff erent parts of the code. How do you know what is x and what is y ? If you include your own notes in<br />

println( ) , there can’t be any confusion:<br />

println( " The x value of the thing I’m looking for is: " + x);<br />

In addition, println( ) can be used <strong>to</strong> indicate whether or not a certain part of the code has been reached.<br />

For example, what if in our “ bouncing ball ” example, the ball never bounces off of the right-hand side of<br />

the window? Th e problem could be (a) you are not properly determining when it hits the edge, or (b) you<br />

are doing the wrong thing when it hits the edge. To know if your code correctly detects when it hits the<br />

edge, you could write:<br />

if (x > width) {<br />

println( " X is greater than width. This code is happening now! " );<br />

xspeed * = – 1;<br />

}<br />

If you run the sketch and never see the message printed, then something is probably fl awed with your<br />

boolean expression.<br />

Admittedly, println( ) is not a perfect debugging <strong>to</strong>ol. It can be hard <strong>to</strong> track multiple pieces of<br />

information with the message window. It can also slow your sketch down rather signifi cantly (depending<br />

on how much printing you are doing). More advanced development environments usually off er<br />

debugging <strong>to</strong>ols which allow you <strong>to</strong> track specifi c variables, pause the program, advance line by line in<br />

the code, and so on. Th is is one of the trade-off s we get using <strong>Processing</strong> . It is much simpler <strong>to</strong> use, but<br />

it does not have all of the advanced features. Still, in terms of debugging, some sleep, a little common<br />

sense, and println( ) can get you pretty far.

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

Saved successfully!

Ooh no, something went wrong!