extending maloney's TestMorph

Lyn A Headley laheadle at cs.uchicago.edu
Wed Sep 26 01:58:02 UTC 2001


I've been trying to extend John Maloney's TestMorph to bounce up and
down forever.  My plan of action was:

- convert into an inst var the index into positions array
- keep track of the object's current direction as 'up' or 'down'
- have the step method either increment or decrement the index or
  toggle the direction if the index has reached an end point.

My attempt is below.  it's quite simple but doesn't work.  The problem
is that Morphic starts stepping my morph as soon as it is created,
resulting in an array out of bounds exception since I don't fill the
positions collection until mouseDown: is called.

I realize I could simply fill the collection at initialization time,
but my real question is: why is my morph getting put on the stepList
and sent step messages?

-Lyn

------------------------------------------------------------------

Morph subclass: #TestMorph
	instanceVariableNames: 'path index direction '
	classVariableNames: ''
	poolDictionaries: ''
	category: 'My Stuff'!

!TestMorph methodsFor: 'as yet unclassified' stamp: 'lah 9/21/2001 20:45'!
direction
 ^direction! !

!TestMorph methodsFor: 'as yet unclassified' stamp: 'lah 9/21/2001 15:47'!
drawOn: aCanvas 
	| colors |
	colors _ Color wheel: 10.
	colors
		withIndexDo: [:c :i | aCanvas
				fillOval: (self bounds insetBy: self width // 25 * i + 1)
				color: c]!
]style[(8 7 4 7 4 6 3 5 8 2 3 6 17 6 2 7 16 4 17 4 10 2 3 1 3 1 13 1 1)f1b,f1cblack;b,f1,f1cblack;,f1,f1cblack;,f1,f1cblack;,f1,f1cblue;,f1,f1cblack;,f1,f1cblack;,f1,f1cblack;,f1,f1cblack;,f1,f1cblack;,f1,f1cblue;,f1,f1cblack;,f1,f1cblue;,f1,f1cblack;,f1! !

!TestMorph methodsFor: 'as yet unclassified' stamp: 'lah 9/21/2001 15:46'!
handlesMouseDown:evt
	^ true! !

!TestMorph methodsFor: 'as yet unclassified' stamp: 'lah 9/21/2001 20:46'!
index
^index! !

!TestMorph methodsFor: 'as yet unclassified' stamp: 'lah 9/21/2001 20:47'!
initialize
	super initialize.
	path _ OrderedCollection new.
	index _ 1.
	direction _ 'up'.!
]style[(10 2 5 14 4 3 17 36)f1b,f1,f1cblack;,f1,f1cblack;,f1,f1cblack;,f1! !

!TestMorph methodsFor: 'as yet unclassified' stamp: 'lah 9/21/2001 20:22'!
mouseDown: evt 
        self startAnimation. ! !

!TestMorph methodsFor: 'as yet unclassified' stamp: 'lah 9/21/2001 20:35'!
startAnimation
	path _ OrderedCollection new.
	index _ 1.
	direction _ 'up'.
	0
		to: 29
		do: [:i | path add: self position + (0 @ (i squared / 5.0))].
	path _ path , path reversed.
	self startStepping! !

!TestMorph methodsFor: 'as yet unclassified' stamp: 'lah 9/21/2001 20:57'!
step 
	direction = 'up'
		ifTrue: [
		   path size = index ifTrue: [
			direction _ 'down']
		   ifFalse: [
			self updateIndex]]
		ifFalse: [
		   index = 1 ifTrue: [
			direction = 'up']
		   ifFalse: [
			self updateIndex]].
	self position: (path at: index)! !

!TestMorph methodsFor: 'as yet unclassified' stamp: 'lah 9/21/2001 20:24'!
stepTime 
        ^ 10! !

!TestMorph methodsFor: 'as yet unclassified' stamp: 'lah 9/21/2001 20:44'!
updateIndex
	self direction = 'up'
		ifTrue: [
			index _ index + 1]
		ifFalse: [
			index _ index - 1]! !




More information about the Squeak-dev mailing list