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

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

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

PROCESSING: CREATIVE CODING AND COMPUTATIONAL ART<br />

336<br />

/* required method implementation<br />

from abstract Creature superclass */<br />

String getCreatureName(){<br />

return "I am a Knight";<br />

}<br />

}<br />

interface Archer{<br />

void shoot();<br />

}<br />

/* game engine-with embedded superclass<br />

reference in the addCreature() method<br />

<strong>and</strong> embedded interface reference in<br />

launchArrow method */<br />

class Engine{<br />

void addCreature(Creature c){<br />

c.castSpell();<br />

}<br />

void launchArrow(Archer a){<br />

a.shoot();<br />

}<br />

}<br />

Sorry if the text output is anticlimactic. Later on in the book, you’ll use OOP <strong>and</strong> polymorphism<br />

to generate some interesting visuals. In this chapter, we needed to get some theory<br />

out of the way—which we’ve now done.<br />

Summary<br />

This was not an easy chapter. OOP introduces many abstract concepts to an already<br />

abstract activity—programming. However, as you begin to create more complex sketches,<br />

the organizational <strong>and</strong> reusability benefits of OOP make it well worth the added complexity.<br />

Also, like any craft-based pursuit, the process gets easier through practice. The first<br />

part of the chapter dealt with the basics of OOP—classes <strong>and</strong> objects. I think you’ll find<br />

working with simple classes in <strong>Processing</strong> straightforward <strong>and</strong> helpful. For example, when<br />

creating an animation, it’s helpful to encapsulate the animated object’s properties (e.g., x,<br />

y, width, <strong>and</strong> height) inside a class, allowing you to target the properties using dot syntax<br />

(e.g., animatedObject.x). In the advanced OOP section, you looked at some pretty complex<br />

concepts, including inheritance, interfaces, abstract classes, <strong>and</strong> polymorphism.<br />

Underst<strong>and</strong>ing <strong>and</strong> implementing these concepts takes time. Fortunately, you can do a lot<br />

without them. However, as you build ever more complex sketches, these advanced features<br />

can provide much greater flexibility <strong>and</strong> efficiency to your sketch design. You’ll be<br />

using most of the features discussed in this chapter throughout the book (to accomplish<br />

more interesting things than getting some text output).

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

Saved successfully!

Ooh no, something went wrong!