In this tutorial we will learn how to shoot a projectile and incorporate the game mechanics required in order to operate the weapons and flow. This tutorial is for absolute beginners. However, experienced programmers may comment and help others.
Series Part 1: http://www.se7ensins.com/forums/threads/xna-4-0-series-the-project-setup.836045/
Series Part 2: http://www.se7ensins.com/forums/threads/xna-4-0-series-content-input-draw-movement.836830/
Skill Level: Easy
Time to Complete: 20 Minutes
Art Assets Used:
#3.1 Scale Rocket Ship
First we need to scale down the size of our ship to 35%.
#3.2 Projectile Class
Now we need to create a new class in our game project. Call it Projectile. This class will be used to store the position and velocity of our projectile.
#3.3 Establishing and Loading Projectiles
First lets declare a new list of live projectiles. This list will contain all projectiles that are bouncing off of the walls. When we add to that list, projectiles will appear in game. When we remove them they will no longer be present in the live game.
Then we must load our projectile content. Add the ball from above to your content project. Follow along to load it in to memory.
#3.4 Shooting Projectiles
Now we will add a new projectile per frame as long as the space bar is down. We set the projectiles position to the ships. We set the velocity of the projectile based on the ships rotation and a set speed. After we do this, we run through the list of live projectiles and update their positions based on the velocity. Take note that we subtract 46 degrees from our rotation. This is because our ships texture isn't default to zero degrees. So we must fix our angle.
#3.5 Drawing our Projectiles
Finally, we must draw our projectile. We draw based on our projectiles position. We want to set the origin of the ball to its center. Then we must scale it down to 20%.
Please take note that we draw our projectiles first, then we draw our ship. The reason for this is because our textures draw in the order we call them. So if I draw the ship first, the projectiles will appear to be on top of the ship, instead of coming out the tip.
Now debug your game and hit the space bar. Your rocket ship will now shoot.
Thank you for reading and following along. If you have any questions please post below. Please watch out for the next parts in the series. This ends the three part series on getting started in XNA. More tutorials will be released.