Game Development Reference
In-Depth Information
So now we have the enemies flying towards the player and disappearing when they
reach the left side of the screen or when they touch the player. We have one more
collision feature to add before we finish up the gameplay.
When the player, moves up or down on the screen they can currently move outside
of the screen, which we don't want. We don't need to do a lot here, and the best
place to put this detection is inside the Player class. However you could still classi-
fy this as collision detection.
Inside Player::Update we need to update the movement code to look like the fol-
lowing:
if (_actionMoveDown->IsTriggered())
{
Move(ShipDirection::Down, deltaTime);
auto bottom = _collider->Position.y +
_collider->Size.y;
if (bottom > screenHeight)
MoveTo(_position.x, screenHeight -
_sprite->Origin.y);
}
else if(_actionMoveUp->IsTriggered())
{
Move(ShipDirection::Up, deltaTime);
if (_collider->Position.y < 0)
MoveTo(_position.x, _sprite->Origin.y);
}
The parts you need to focus on here are the lines after the Move calls. If the player
moves down, we need to make sure the bottom of the image doesn't cross below the
screen. When moving up, we need to ensure the vertical position doesn't go below
zero, or the top of the screen. If the player goes past any of these points we just lock
the player to the edge of the screen until it moves in the opposite direction.
Now if you run the game, the player should be locked within the screen and enemies
should spawn and move to the left until they pass the edge of the screen or hit the
player.
Search WWH ::




Custom Search