A shooting game would not be much fun without having images that move. In this lesson you will learn how to move your images by sending messages to your Morph objects.
1. Let’s continue by using the Workspace from your last lesson. You can continue to use the objects that you created in it.
2. In the Workspace window, type the following code and then “do it”:
b position: b position.
3. What happened? Did you notice any change? Can you tell by looking at the message that was sent to your EllipseMorph?
4. Your EllipseMorph did not appear to move because you told it to move to its same position. Nothing new to see.
5. Let’s try something different this time. Type the following code in your Workspace window and then “do it”:
b position: (b position) + (10 @ 0).
6. Did you notice anything different? What if you run this code a few more times? What do you see? You may have even noticed that your EllipseMorph moved outside of your RectangleMorph. This is because boundaries have not been set yet. If this happens, you can center your Morph using the expression from the last lesson.
7. The 10 @ 0 represents a point with a X coordinate (location) and a Y coordinate (location). The values for X move a point left or right. The values for Y move a point up or down. In step 5, you sent a message to your EllipseMorph directing it to move right.
8. Let’s try more examples like this. Type the following code in your Workspace window, then “do it” for each line. Try running these multiple times. What do you see?
b position: (b position) - (10 @ 0).
b position: (b position) + (0 @ 10).
b position: (b position) - (0 @ 10).
9. In the previous step, you moved the EllipseMorph right, left, down, and up. Let’s try a couple more examples. Type the following code in your Workspace window and then “do it” for each line:
b position: (b position) + (10 @ 10).
b position: (b position) + (-10 @ -10).
10. What did you see this time? Why did your EllipseMorph move like this?
11. You have learned a lot of ways to move your Morphs around. For your shooting game, your enemies will only move right or left. Let’s discover another way to send messages to your Morphs. Type the following code in your Workspace window and then “do it” for each line:
b x: b x + 10.
b x: b x - 10.
12. What do you see? Can you figure out what is happening by looking at the messages that were sent to your EllipseMorph?
13. Save and Quit your Smalltalk image.