[ANN][Squeak-dev Image] Version 07.6

Blake blake at kingdomrpg.com
Tue Jun 19 08:35:10 UTC 2007


OK, the graphic system in Squeak is called MORPHIC. Whereas before you  
descended your new classes from "Object", now you descend them from  
"Morphic":

Morph subclass: #TestMorph
	instanceVariableNames: ''
	classVariableNames: ''
	poolDictionaries: ''
	category: 'Max-Morphic'

When a morph wants to draw itself, it calls the drawOn: method. You would  
override this method to make the morphic look like something. For now, our  
morphic will just look like a blue square (the default drawOn does that),  
so we won't have a drawOn method.

OK, now that we have a new morph, let's create an instance. But morphs  
don't exist in a vacuum, they have to exist in a morphic world. Try this  
in a transcript:

TestMorph new openInWorld.

When you "do it", you should see a blue square in the upper left of the  
screen. The morphic world controls when the morphs that are in it (the  
morphs that it "owns") move. A morphic that responds to the passage of  
time is said to be "stepping". If a morphic is stepping, the world calls  
its "step" method.

So, create a step method:

step
		self position: self position+(1 at 1).

A morphic has a position (or "point") on the screen. Whaat this line of  
code does is say "take the current position and add one to its x position  
and one to its y position". This will move it diagonally. So, if you save  
this, the morphic should start moving, slowly, diagonally toward the lower  
right.

We want to be able to control how the movement starts and stops, so we'll  
make the morphic handle mouse events. We do this by overriding the  
handlesMouseDown:

handlesMouseDown: evt
	^ true

then having a mouseDown event:

mouseDown: evt
         	(self isStepping) ifTrue: [self stop] ifFalse: [self start].

"isStepping" is a Morphic event that retruns true if the morphic is  
stepping (responding to time events, remember?), start causes a morphic to  
start stepping, and stop causes it to stop.

So, if you click on the morphic now, it'll start moving unless it is  
moving already, in which case clicking on it will cause it to stop.

The morphic will probalby be moving slowly. I think, by default, a morphic  
world sends one message per second to a morph. But you can request more  
frequent updates with the stepTime method:

stepTime
	^100

The amount returned in stepTime is the number of milliseconds between  
calls to step. So, if yous et it to 1000, our morph will momve about once  
a second (slow). The above is 100 milliseconds, or ten moves a second. On  
my machine, returning a value of 15 is about as fast as it will go. (Less  
than 15 shows no change.) I think "0" means "Go as fast as you can".

In game programming, the timing of steps is critical, so you'll figure out  
how fast the fastest thing can go, and base everything around that.

	This should get you started.



More information about the Squeak-dev mailing list