Events

Exercise 8.1

The method handlesMouseOver:, implemented in the SpaceWar morph class, returns true so the game play is informed of mouse over events in dedicated methods.

SpaceWar>>handlesMouseOver: event
   ^ true

Exercise 8.2

You need to browse the Morph>>handlesMouseOver: method and read the comment. It writes about a #mouseEnter: message; we implement the matching method in SpaceWar class with the behaviors previously described:

SpaceWar>>mouseEnter: event
   event hand newKeyboardFocus: self.	
   self startStepping

Exercise 8.3

The message #mouseLeave: is sent to our SpaceWar instance each time the mouse cursor move out (leaves) of the game play. Therefore we add the homonym method to the SpaceWar class:

SpaceWar>>mouseLeave: event
   event hand releaseKeyboardFocus: self.
   self stopStepping

Exercise 8.4

The #handlesKeyboard message is sent to a morph to know if it wants to receive keyboard event. The morph responds true to this message to state its interest on keyboard event. We implement the method in the SpaceWar class:

SpaceWar>>handlesKeyboard
   ^ true

Exercise 8.5

We designate the characters as $w $a $s $d. We append the code below to the method SpaceWar>>keyStroke:

key = $w ifTrue: [^ ships second push].
key = $d ifTrue: [^ ships second right].
key = $a ifTrue: [^ ships second left].
key = $s ifTrue: [^ ships second fireTorpedo]