Is it going so far so in the last lesson, we identified a problem because we can move off of our grid
and we need to set the boundaries that the player needs to stay within our game area, otherwise it
could go pretty much anywhere.
So we need to set those restrictions.
That's what I'm going to do in this lesson.
So opening up where we handle our key press, so setting a condition.
So checking to see if the player pressed left and if they did, then we're checking the value of box
X.
So this is the variable within box where we're tracking the position of X and we're checking to see
if it's larger than game area.
And remember, we got that bounding client bounding rectangle value.
So that's the game area left.
So making sure that if we're going left, we're not outside of the left area.
So now, even if I press left, we're not doing anything because we're not able to move left anymore
and we've got to do the same thing for right.
So the same idea here where we're taking the value of box X and we going to make sure that the value
of X is less than we got to do.
Some little bit of calculation here because we can do game area.
Right.
But what that's going to do is it's not going to actually account for the width.
So if we can still go over, but we can't go over too much, so we can still go out of balance, we
want to still restrict that.
So we've got a little bit more calculating and lucky for us, we can get the width of our box elements.
So clearing that out, we've got our box object and we can see that we can get the offset width of that,
which is of course, one hundred, because that's how wide it is.
And we try to keep it, of course, dynamic as possible.
So subtracting the value of box.
Offset with should account for that ability to go one more over.
So now we are restricting it to that boundary area so that we can't go off of the edge and we need to
do the same thing for down and as well as up.
So taking down, checking to see if this time we're using box, why is less then and we're using that
same game area and it's going to be game area bottom.
So let's try that out.
So refresh and going down.
So we see we had the same problem here that we can go past what the bottom areas and we need to make
that same adjustment accounting for the height of the box.
So add in some extra brackets there and we're going to subtract the value of box and offset height this
time.
So that will account for the height of the box and we'll just make sure that it works.
So now we can't go below 17 and we've got one last one to take care of and that's the up.
So checking to see if box Y is greater than game area top.
And try that one more time, so I can't go up, I can't go out of bounds and exactly what we want to
be able to do and we see that we're still tracking the squares properly.
So that's going to be essential in the upcoming lesson where we start activating our books in order
to navigate to to in order to score within this game.
So go ahead and add the boundaries into your project so that you can't the player can't exit out of
or get out of bounds of our gameplay area.
And coming up next, we're going to add in our active object that we need to pursue.