Game Development Reference
In-Depth Information
let's implement the code to do this movement. Change to Ship.cpp and implement
Ship::Move , as shown in the following code:
void Ship::Move(ShipDirection direction, float
deltaTime)
{
auto amount = deltaTime * _speed;
switch (direction)
{
case ShipDirection::Up:
_position.y -= amount;
break;
case ShipDirection::Down:
_position.y += amount;
break;
case ShipDirection::Left:
_position.x -= amount;
break;
case ShipDirection::Right:
_position.x += amount;
break;
};
if (_sprite != nullptr)
_sprite->Position = _position;
}
No matter what direction the ship is moving in, it will always move a certain amount in
the frame, so we can calculate that up front by simply multiplying the speed with the
time since last frame ( deltaTime ). Now we just use a switch statement to add that
amount to the position based on the chosen direction. For ShipDirection::Left
and ShipDirection::Right , we need to subtract and add, respectively, and for
Up / Down we need to do the same thing: subtract and add. After that we need to en-
sure this new position propagates to the Sprite object so that when we get around
to drawing the texture, it will show up in the right place.
Search WWH ::




Custom Search