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 releaseKeyboardFocusIf: self.
   self stopStepping

Exercise 8.4

The #handlesKeyboardFocus message is sent to a morph to know if it wants to receive the keyboard focus. By default, the morph responds true to this message to state its interest on keyboard event. Therefore, we don’t need to implement it in the SpaceWar class:

Morph>>handlesKeyboardFocus
   ^ true

To be more precise, our game manually sets and unsets the keyboard focus when the mouse enters or leaves the game area. In this case, even returning false to this message does not affect the behavior.

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]