Smalltalk Programming
Lesson 17

Your enemy will not be much of a challenge if it does not move. Let’s start working on its ability to move in your shooter game. Today, you will make the necessary code changes to move your enemy and enable Morphic animation for your enemy.

1. Type the code below and save the changes for Enemy>>move.

move

    
self right: self right + 10

2. Test your code changes by running the expression “self move.” on your enemy object in an explore morph window.

3. What do you notice about your enemy with your new code?

4. You may have noticed that your enemy does not stay within your shooter game screen. Make the following code change and save it in order to correct this issue. If your enemy instance is out of the game screen, you can run the expression “self x: owner x.” in your explore morph window on your enemy instance or even “self x: self x - 10.”. You will want to make sure that your enemy is within your shooter game screen before trying your code change below.

move

    
self right: self right + 10.
    
(self right > owner right)
        
ifTrue: [self left: self left - 10]

5. Test your code changes by running the expression “self move.” on your enemy object in an explore morph window.

6. What do you notice about your enemy with your code changes?

7. You noticed that your enemy will now stop before leaving the right side of the game screen, however the enemy does not move left. This creates an interesting problem – how to move the enemy left instead of right? How will your method know which direction the enemy needs to move? Every time your method runs, it will not know which direction your enemy is moving – it can only find out where the enemy is on the screen. From what you have learned, can you think of a possible solution to this problem? Try to think of a possible solution for the next lesson.

8. For now, go ahead and implement Morphic animation for your enemy instances by adding the following 2 methods.

step

    
self move

stepTime

    
^ 1500

9. Do you remember what these 2 methods do?

10. You do not need to start stepping on your enemy instance. This is not needed now and can be done later.

11. What else needs to be done for your shooter game?

12. Save and Quit your Smalltalk image.

Download the PDF version.