Game Development Reference
In-Depth Information
{
auto moveAmount = deltaTime * _playerSpeed;
if (_sidePressed > 0)
{
_player->Y = _player->Y + moveAmount;
}
else if (_sidePressed < 0)
{
_player->Y = _player->Y - moveAmount;
}
}
This method is mostly straightforward. We will take the value of _sidePressed and,
based on whether it is less than or greater than zero, we subtract or add the right
amount of movement to the Y component of the _player object.
The first line of the method calculates how far the sprite should be moved in this
frame. If we just move each frame by a fixed amount, we would have a different ex-
perience when the frame rate fluctuated. Imagine the sprite moving 10 pixels every
iteration. Now if the game runs at 30 frames per second, the sprite would move at
300 pixels per second; however, if the player has a machine that can handle the
game at 60 frames per second, we will suddenly have moved double the distance in
the same amount of time.
To resolve this we need the timing information generated by the BasicTimer class
in the GameApplication class. Here we can retrieve the time that has passed
since the previous frame as a fraction of a second, and simply multiply it by the dis-
tance to move in a second to find out our final value. This way we can specify a
single value for the distance to move in a second, and be sure the sprite will move at
that rate for any number of frames per second.
Now we have everything in place, and we just need to connect the Game class to
the GameApplication class again before anything will work. To do this, add the
following code to OnPointerPressed , and similar code to OnPointerMoved and
OnPointerReleased .
Search WWH ::




Custom Search