Game Development Reference
In-Depth Information
the animal is in a hole in the ice;
the animal is not visible;
or, the animal is already moving.
In all of these cases, we do not do anything and we return from the method:
if (! this .Visible || this .boxed || this .velocity != Vector2.Zero)
return ;
If we did not click on the animal (in other words: the mouse left button was not
pressed or the mouse position is outside of the bounding box of the animal), we can
also return from the method, so the complete if -instruction then becomes
if (! this .Visible || this .boxed || this .velocity != Vector2.Zero ||
!inputHelper.MouseLeftButtonPressed() ||
!BoundingBox.Contains(( int )inputHelper.MousePosition.X,
( int )inputHelper.MousePosition.Y))
return ;
If we did click on the animal, we need to make the selector visible, set its position,
and assign the animal as the target animal of the selector. This is covered in the
following instructions:
AnimalSelector selector = GameWorld.Find("animalselector") as AnimalSelector;
selector.Position = this .Position;
selector.Visible = true ;
selector.SelectedAnimal = this ;
Now we can select animals at will and tell them to move in a particular direction.
What we need to do now is handle the interaction between the animal, the playing
field, and other game objects.
23.3 Input Handling Order
The order in which we draw objects on the screen is important. For example, if we
draw the penguins before we draw the background image, the player will never see
the penguins. Until now, we did not really pay attention to the order in which the
HandleInput and Update methods are called. For instance, in the GameObjectList class,
this is how we call the HandleInput method on all the child objects:
foreach (GameObject obj in gameObjects)
obj.HandleInput(inputHelper);
In most cases, this approach works just fine. But in the case of the Penguin Pairs
game, we may get weird behavior. Suppose that two penguins are next to each other
Search WWH ::




Custom Search