We’re going to explore a second scenario where we need to discover details of the world as the program is running and calculate specific distances so that our movements look realistic.
Here’s the scenario:
We have an Angel and an Egyptian, standing an unknown distance from each other. Move the Angel to the Egyptian so that they can comfortably talk together.
Download the world at the bottom of the screen: Mod7.4AngelAndMoveableEgyptianStarterWorld.a2w
Open up the AngelAndEgyptian world. Let’s add some code into World.My first method to achieve this… use the move_to method of the Angel, to move it to the Egyptian. Once you have created the code, run the program.
what the code should look like:
Does this look realistic? Is this how you would expect an Angel and an Egyptian to talk to each other?
It’s not too realistic, is it? The Angel has splatted right onto the Egyptian, and I think only really really close friends would talk to each other like that.
It would be much better if the Angel came close to, but not on top of, the Egyptian. Ok, how do you do that? You could use the move … towards … method where you specify the distance you’re going to move, and towards whom you’re moving. Try it now – change your move_to method to the move … towards … method. Adjust the distance until you get it right (just by guessing number values as we did at the beginning of this section).
How many times did you have to change the distance until you got it right? It took us about 4 times. Tedious, eh – but at least it was possible. We’re going to extend the scenario now so that this method of “try-and-see” as we write the program won’t be possible -- we'll have to use mathematical expressions to solve our problem. Here’s the new scenario:
The Egyptian is free to move around the world (he is moved by pressing the up, left and right arrow keys to move forward, turn left and turn right respectively). Sometimes, he wants to speak with the Angel and so he calls her (pressing the Enter key produces a speech bubble above the Egyptian "Oh, Angel!"), and brings the Angel right to him.
[Q7.4.1]: Why can’t we use the same “try-and-see” technique for moving the Angel to the Egyptian in this scenario as we did in the earlier example above?
Open up the world AngelAndMoveableEgyptian. If you run it, you’ll see that you can move the Egyptian around the world using the arrow keys, as described in the scenario. Now let’s create the event handler that will move the Angel over to the Egyptian.
1. Click create new event to create a new event handler.
A. Click “When a key is typed”.
2. Set “any key” to “Enter”.
3. We don’t yet have a method to call when the Enter key is pressed
A. Select the "world" object in the top left corner (Object Tree pane).
B. Click on the create new method button in the lower left corner (Details pane).
C. In the New Method dialog box, type "AngelGoToEgyptian" to create a new world method called “AngelGoToEgyptian”.
D. Click OK.
4. Select the Angel in the top-left panel.
5. In the world's details pane, select and drag the move…towards… method to your new AngelGoToEgyptian method.
6. For now (even though we'll change it later), pick 1 meter and the egyptian as the parameters to the method.
7. We need to change the 1 meter we just selected to the correct distance to move.
A. That has something to do with the distance between the Egyptian and Angel.
8. Select the Angel in the top left pane.
9. Click the functions tab in the lower left corner.
10. Drag the distance to function on top of the “amount = 1 meter” parameter.
A. Select the egyptian as the parameter.
11. Now look back to the "When enter key is typed" event handler and change do "Nothing" to do the "AngelGoToEgyptian" method.
Now run the code. What do you get?
Hmmm. We seem to be back where we started. The angel has moved right on top of the Egyptian. We need to move the Angel a shorter distance.
[Q7.4.2]: We can discover the full distance between the two actors with the distance_to function. What arithmetic (math) could we perform on this full distance in order to get the Angel to move the right amount?
For the Angel and the Egyptian to talk comfortably, they need to be around 1 meter apart. So, no matter what the distance between the two, if we take 1 meter off that value, then we'd have the right distance for the Angel to move. The centers of the Angel and the Egyptian would be 1 meter apart then.
Let's create a calculation based on the distance they are apart:
The "amount" parameter currently contains a single tile - the angel distance to egyptian function.
2. Click on the little down-arrow on the right edge of the "amount" parameter tile (NOT on the Egyptian tile that is contained in the function call).
3. Select "math" then "angel distance to egyptian -" and then "1 meter"
So now we’ve created a calculation (or mathematical expression) using the subtraction operator. Change the “1 meter” to other values to convince yourself that this is working as you expected. Also, make the Angel turn towards the Egyptian before it moves, in order to make the action look more realistic.
Don't forget: Download the file linked below. Launch Alice on your computer, then use File -> Open World and select the file you downloaded.
Mod7.4AngelAndMoveableEgyptianStarterWorld.a2w
Save the world as LastNameFirstName-7.4AngelAndMoveableEgyptian.a2w