Smalltalk Programming
Lesson 9

You starting creating your ship in your last lesson. It was determined that you will need more code so that your ship will become part of the game and not a random object on your screen. Today you will add the code to make the ship become a part of your game.

1. In your ShooterGame class, add the new method below. You will notice something different this time when you try to save it. Proceed to the next step to see what happened.

initializeShip

    
ship := Ship new.
    
self addMorph: ship

2. You will notice this time that when you tried to save your changes to the initializeShip method, you received a popup box. If you read the title of the popup box, it says that ship is an unknown variable in your code. Select “declare instance” and then press the “Choose” button.

Popup box for unknown ship variable

3. Declaring the ship instance variable will give ShooterGame a good way to access and keep track of your ship.

4. Look at the code for your ShooterGame class. You will see that there is now a ship instance variable. You did not have to type this since Smalltalk added it for you when you used the popup box to declare it. You could have also added the ship variable to your ShooterGame class yourself, and then created the initializeShip method afterwards. Or, you could have even ignored the warning, saved your initializeShip code, and then added the ship variable to your class code.

5. Now make the following changes to your ShooterGame initialize method and then save the changes.

initialize

    
super initialize.
    
self position: 100 @ 100.
    
self extent: 640 @ 480.
    
self color: Color black.
    
self setNameTo: 'Shooter Game'.
    
self initializeShip

6. What do you notice about the ship and your shooter game? What else needs to be done?

7. Save and Quit your Smalltalk image.

Download the PDF version.