Let's explore the car world we just saw. Download Magnet World with Other Objects at the bottom of the page.
Click play and test to make sure this game is working how we expect (remember that the car moves to any object that you click on).
Does it work? Did you see that the car will also move to a tree if you click on a tree?
Let's revisit the code and make sure we understand why it is moving the car to any object that we click on. Remember we have two methods, my first method and feel the force. my first method gets called when we click play and the car keeps moving forward for a long time. feel the force gets called from the click event and changed the direction that the car is driving:
Events:
feel the force method:
The problem with this game was that the car would move toward any object that was clicked on, not just the magnets.
Below is a video of how the program currently behaves. Notice that the car moves towards any object that is clicked on (same as video 1 of previous page):
In this section we are going to change the program so that when you click on a magnet, the car moves towards the magnet, but if you click on anything else the car will not change directions to move towards that object, it will keep moving towards the last magnet you clicked on (same as video 2 on previous page):
Before allowing the car to move, we need to check to make sure one of the magnets was clicked on.
To do this we are going to add an if/else statement to our feel the force method and put all the feel the force code in the if/else statement so that the car only moves if one of the four magnets is clicked.
Rather than always executing the code in the feel the force method we want to conditionally execute the turn to face method if one of the magnets is clicked (if some other object was clicked, the car shouldn't turn... but continue in the same direction -- controlled by the long duration move in my first method.
We don't want to restrict the car to only move toward one magnet, which means we need to check to see if:
the object that the mouse clicked is magnet OR
the object that the mouse clicked is magnet2 OR
the object that the mouse clicked is magnet3 OR
the object that the mouse clicked is magnet4
This is a complex sentence and to write this in code we need to first break it up into smaller parts. Eventually we will build this:
(click here to see larger image)
But let's build it one piece at a time.
There is an easy way to check this, we need to use a compound boolean expression, or in this case, an either/or/orBoth tile**:
Let's add one either/or/orBoth tile into our if statement. To do this the true (or false) you picked as a default value when you dragged in the if/else tile. Click on the world object in the upper left window, click on world's functions tab below that, and at the very top of the set of functions, select (under boolean logic) the either a or b or both tile. Then drag that on top of your default true (or false) expression controlling the if/else statement.
Let's first just check to see if the object the mouse clicked is either magnet or magnet2. We know that the mouse clicked the first magnet if our parametermagnet is our object magnet:
To do this we drag our parameter target into our first true part of our either/or/orBoth tile. When we drop target on top of true a menu will pop up, choose target == so that we can compare if parameter target is equal to object magnet. Then choose magnet from the object list to compare to parameter magnet:
Do the same for magnet2. Now your code should only move the car towards an object if that object is either magnet or magnet2:
[Q9.2.1]: What do you think will happen when you press play?
Oops! The code does the exact same thing as when we started! This is because we didn't move the car turn to face instruction into the if statement!
[Q9.2.2]: Should the convertibleCorvette instruction be placed inside of the "if" portion or the "else" portion of the tile?
Put the convertibleCorvette instruction where it belongs.
[Q9.2.3]: What do you think will happen when you press play and click on magnet? magnet3? A tree?
Go ahead and click play and make sure the code does what we expect it to (only moves towards magnet and magnet2 if they are clicked on).
[Q9.2.4]: Our either/or/orBoth tile says or both, can the parameter target ever be magnet and magnet2? Why or why not?
Let's explore a different example where or both will comes in handy:
Suppose you are creating a program that has a person waiting for two guests to arrive at their house. The person will want to open the door when guest1 arrives or guest2 arrives. But it is also possible that both guests arrive at the same time, so the person will need to open the door when either guest1 or guest2 or both guest1 and guest2 have arrived.
For our car game, we know that parameter target will never be both magnet and magnet2, but it doesn't make our if/else wrong if we check.
But we still need to check if the user clicked on magnet3 or magnet4. To do this, we should revisit what we need our if/else statement to check:
the object that the mouse clicked is magnet OR
the object that the mouse clicked is magnet2 OR
the object that the mouse clicked is magnet3 OR
the object that the mouse clicked is magnet4
We know that our either/or/orBoth tile can only compare two things at a time, but we need to compare four things.
How can we group our four checks above into comparisons of 2?
[ the object that the mouse clicked == magnet OR
[ the object that the mouse clicked == magnet2 OR
[ the object that the mouse clicked == magnet3 OR
the object that the mouse clicked == magnet4
] or either
] or either
] or either
Let's add in the check for magnet3, then we can do magnet4. Try changing our if/else statement to check if our parameter target (the object that the user clicked on) is Either magnet Or magnet2 Or magnet3 then move the car towards the magnet that was clicked on. Else, do not move the car.
To do this, we need to add to the either/or/orBoth statement that we already have. Click on the down arrow just to the right of the orBoth in the second condition, then select from the popup window to add another either/or/orBoth tile. This will add a third "true" to your expression, which you then replace as you did above -- but to check if parameter target is magnet3.
Now add the final boolean expression for magnet4. Your boolean expression should now look like the image below (same image we showed you earlier):
(click here to see larger image)
Click play. Did that work? Does the car move towards the magnet that was clicked on? Does the car move when something other than a magnet is clicked on?
** You could instead use 4 different events. You wouldn't want to do this because then you would have to create 4 events (one for each magnet) and if you wanted to allow the car to drive towards other objects you would have to make an event for each object. This starts to get really complicated in terms of events, and it is better to have 1 event that calls 1 method that allows you to move to certain objects. It also makes it easier to identify what objects the car can go to (you only have to look at one tile versus having to go through all the events you may have).
Save the world from this page as M9-Magnets-pg2-Compound-YourName.a2w