6.5 Building your specialized Morph

Let’s make a simple subclass which changes color when Left-Clicked. Create a new class just as we did with Spacewar! but subclass EllipseMorph with #ColorClickEllipse.

EllipseMorph subclass: #ColorClickEllipse
   instanceVariableNames: ''
   classVariableNames: ''
   poolDictionaries: ''
   category: 'Spacewar!'

Save the class definition with Ctrl-s.

Right-Click on the Message Category pane and select new category.... This brings up a number of selections and allows us to create new ones. Select “event handling testing”. Then add the method ColorClickEllipse>>handlesMouseDown:.

handlesMouseDown: aMouseButtonEvent
  "Answer that I do handle mouseDown events"
  ^ true

Likewise, add a new catagory “event handing” and add the other method we need.

mouseButton1Up: aMouseButtonEvent localPosition: localEventPosition
   "I ignore the mouseEvent information and change my color."
   self color: Color random

Now, you have created a new Morph class and can select a ColorClickEllipse from the World Menu New Morph.. and try it out. These are fun to Left-Click on. Make as many as you want!

ch06-15-ColorClickEllipse

Figure 6.13: Obtain a ColorClickEllipse

Now you know how to specialize an individual morph, or make a whole new class of them!