Lots of concurrency

Ned Konz ned at bike-nomad.com
Sat Oct 27 00:06:14 UTC 2001


On Friday 26 October 2001 03:40 pm, Jerry Balzano wrote:
> Is a game like this easy to create in Squeak?  It seems like it ought to
> be, but I seem to remember reading somewhere that "keyboard focus" is
> generally held by one morph at any given time.  How then would one handle
> the problem of allowing each player to have different "move" and "shoot"
> keys where different morphs are "looking for" different keys all the time,
> at the same time?

There's two possibilities that I can think of offhand.

First, the event routing is to the Morph with keyboard focus, if any, 
otherwise to the World. Which could route the messages as needed.

But perhaps easier, you can just do:

World currentHand addKeyboardListener: someMorph

and someMorph would get sent handleListenEvent: with a copy of the event. So 
you could have each player morph listening to the keys and only responding to 
the ones it recognized.

> If there were some way that an explanation/mini-lesson on a squeak version
> of this game could be provided by one of the kind folks in this discussion,
> and also perhaps serve as a more kid-friendly example of concurrency, that
> would certainly "feed two birds with one burger" for me!

Ok, try the attached change set (it's not a full demo, but it shows the 
keyboard hooking).

-- 
Ned Konz
currently: Stanwood, WA
email:     ned at bike-nomad.com
homepage:  http://bike-nomad.com

--------------Boundary-00=_EA8U099IP14KP0BTZILM
Content-Type: text/x-java;
  charset="iso-8859-1";
  name="KeyboardListenerDemo-nk.1.cs"
Content-Transfer-Encoding: 8bit
Content-Disposition: attachment; filename="KeyboardListenerDemo-nk.1.cs"

'From Squeak3.2alpha of 3 October 2001 [latest update: #4441] on 26 October 2001 at 5:04:48 pm'!
"Change Set:		KeyboardListenerDemo-nk
Date:			26 October 2001
Author:			Ned Konz

KeyboardTestMorph demo

This shows how you can use addKeyboardListener: to hook keyboard events."!

Morph subclass: #KeyboardTestMorph
	instanceVariableNames: 'keyMapping '
	classVariableNames: ''
	poolDictionaries: ''
	category: 'NK-Demo'!

!KeyboardTestMorph methodsFor: 'as yet unclassified' stamp: 'nk 10/26/2001 16:56'!
handleListenEvent: evt
	| mapping |
	mapping _ keyMapping at: evt keyValue ifAbsent: [ ^self ].
	mapping value: self! !

!KeyboardTestMorph methodsFor: 'as yet unclassified' stamp: 'nk 10/26/2001 16:51'!
initialize
	super initialize.
	keyMapping _ IdentityDictionary new.
	self currentHand addKeyboardListener: self. ! !

!KeyboardTestMorph methodsFor: 'as yet unclassified' stamp: 'nk 10/26/2001 16:56'!
on: keyCharacter do: aValuableObject
	keyMapping at: keyCharacter asInteger put: aValuableObject.! !


!KeyboardTestMorph class methodsFor: 'as yet unclassified' stamp: 'nk 10/26/2001 17:03'!
demo
	"KeyboardTestMorph demo"
	"Opens up two KeyboardTestMorphs that you can control with the arrow keys"

| m1 m2 |

m1 _ KeyboardTestMorph new.
m1 on: Character arrowDown do: [ :k | k position: k position + (0 at 1) ].
m1 on: Character arrowUp do: [ :k | k position: k position + (0 at -1) ].
m1 position: 20 at 200; openInWorld.

m2 _ KeyboardTestMorph new.
m2 on: Character arrowRight do: [ :k | k position: k position + (0 at 1) ].
m2 on: Character arrowLeft do: [ :k | k position: k position + (0 at -1) ].
m2 position: 200 at 200; color: Color red; openInWorld.! !


More information about the Squeak-dev mailing list