Game Development Reference
In-Depth Information
The final case you need to cover is if the player touched or clicked the animal but the animal selector
was already visible. In that case, you don't have to do anything, and you can return from the method:
var animalSelector = this.root.find(ID.animalSelector);
if (animalSelector.visible)
return;
Now that you've handled all the cases, you can make the selector visible, set its position, and assign
the animal as the selector's target animal. These are covered in the following instructions:
animalSelector.position = this.position;
animalSelector.visible = true;
animalSelector.selectedAnimal = this;
As you can see, properly handling user input can be complicated sometimes. You need to take care
of all possible actions that a player can take and handle the input appropriately. If you don't do this
right, you risk introducing bugs in the game that can lead to a crash (which is bad) or to a possibility
for the player to cheat (which is even worse, especially in online multiplayer games).
The instructions you just wrote allow the player to select animals at will and tell them to move in a
particular direction. Now you need to handle the interaction between the animal, the playing field,
and other game objects.
Handling Input in Reverse Order
The order in which you draw objects on the screen is important. For example, if you draw the
penguins before you draw the background image, the player will never see the penguins. However,
the order in which objects handle input shouldn't be the same as the order in which they're drawn! In
the Penguin Pairs game, this would lead to unexpected behavior.
Suppose two penguins are next to each other on the playing field, and you click one of them. Then
four arrows appear. Because the two penguins are next to each other, one of the arrows is drawn
over the other penguin (see Figure 22-3 ). If you click that arrow, what happens? Does the selected
penguin move to the left, or do you select the other penguin?
 
Search WWH ::




Custom Search