Welcome back.
How's your project coming along in this lesson?
We're going to be taking care of the shooting.
So we've got player fire has been set to true if we press any of these keys and then within the update,
we can check to see if that player fire is true.
And if it is, then we can take an action accordingly to that.
So setting that up and we can look to check to see if player fire is true.
If it is, that means that we have to move our projectile.
And there's a few other things that we still need to do as well, in addition.
And that's actually set up the firing object.
So we need to unhide remove the class of hide from it.
We need to set the player fire as well and also the position of where it's starting.
So creating a function in order to handle that, we can call that ad shoot and then this is the function
that's actually going to get invoked whenever the player presses any of these keys.
So we can keep that in here where we do add, shoot and also remove out that player fire.
So we're going to have this happen one time.
And if this is the case that it is true, we want to make sure that we're selecting that elements and
updating its class list, removing the class of height.
So it's going to show it on the screen.
So see what happens.
So now when I hit Start Game and if I press the space or if I press up, you see that the shooting object
appears, the projectile appears.
So the next thing that we need to do, we need to select that object and we're going to create a value
of X in that object that we can call it X position or whatever we want to call it.
This is a brand new value that we're setting and then taking the my ship offset left.
So just as we did in the last lesson, this is going to give us a position of where the ship is located.
And you can try that as well in the console.
And you can see that that's going to return back where its position is according to where it's located
on the screen.
So this will change depending on where you move it.
And then if we go back to here and if we offset at sea change to 69 there.
So we're getting the midship offset left position.
And then what we want to do is we want to start at a roughly halfway into my ship and the way that we
can do that is using the offset width and this time dividing it by two.
So that will give us roughly halfway positioning of where our exposition is and then also for the Y
position.
So this one is going to be a little bit easier to set because we're just using the position of the ship.
So this is going to be our default position of where the firing is starting and we can use offset top
and do a minus 10 on that.
So the next thing that we want to do is we don't want it showing up here on the top left hand corner.
We want it to show up.
And this is where we update its style properties.
So updating its left position.
So just like we did with the ship, we're taking fire me.
And this is X position.
Plus pics, and this one should be the Y position and to do adjust the vertical, we're going to use
top and Y position.
So let's see what happens.
So hit start.
And now this is the starting position of where it's going to place place it.
And as you see, when I move it, if I hit spacebar than it, it places it there on top of my my ship.
So wherever I've moved, that's where the starting position is.
So the next thing that we need to do is create that action.
And we already saw that in the last lesson where we could move the ship around and we can do the same
thing and we can have this essentially firing as it's being projected out.
And the other thing, too, that we want to listen for is we don't want to have the ability to add more
shots so we can run the function or we can check to see if the value of fire or the value of player
fire.
And if player fire is false, then we're OK to add another shot.
So let's see what happens.
So now I should only be able to add at one time and it's not going to move.
So that's what we want to do, because otherwise the player could constantly press the spacebar.
It would readjust the positioning.
So now we're ready to make the move with it and taking the fire me x position that we just set earlier.
And actually we don't have to update the exposition.
We only have to update the Y position.
So taking that value and we can subtract it by a value of 15 and then take fire me.
And selected style top and using that newly adjusted position, we can do y position and then don't
forget to add the picks.
Otherwise, it's not going to work.
So see what happens now.
So I hit the spacebar.
It goes off the top.
So we also want to check to make sure that the position is of fire me is not less than zero.
And if it is that we just need to restart it.
So we need to check to see if Fiame is greater than zero.
And if it is, then we're good to subtract off of it.
But if it's not, then that means that we've gone off screen and it was a miss so we can change the
play fire boolean value back to false and that will reset it and allow the player to fire again and
then fire me.
And as we.
Remove the class of hide, let's add in hide, so it's no longer going to be visible on the screen for
the player.
So even though it's right now it's going to be off the screen, the player either way isn't going to
be able to see that.
And we can also, just to be sure, we can readjust the position of fire me y position and we can set
it to use in the container demn height plus one hundred so we can set its default position way outside
of the visible area for the player and fire me y position is the one that's greater than zero.
So we've got to make sure that those variables are checked properly.
So now we're ready to try that out.
So now when we start the game, we can fire, but we can only have one projectile on screen at a time.
And you can see that once it goes off the screen that I can launch another one.
So it does look fairly quick, but there's only one on the screen at a time and it's always the same
one.
It's that same element that we created.
So go ahead and add this into your project and we're going to be ready to move on to the next step.
We're going to clean up the start game and initiate the creation of the aliens, the bad guys.
That's all.
Still to come, an upcoming lesson.