XNA Creators Club Online

beginner's guide

2D Lessons

1 Introducing the 2D Tutorial
2 Creating the Game Project
3 Adding Assets
4 Drawing the Background
5 Creating a Cannon
6 Firing Cannonballs
7 Adding Enemies
8 Destroying Enemies
9 Keeping Score
x Extra Credit - Community Tutorials

6. Firing Cannonballs

Learn about vectors as you create an array of cannonballs that can be launched by the player's cannon and fly across the game screen.

Step 1

Watch Video
Add new variables to your GameObject class to allow for objects that have velocity and that can toggle between alive and dead modes.

Step 2

Watch Video
Create and initialize an array of new cannonball GameObjects that can be fired by the player.
More Details

Step 3

Watch Video
Draft a new UpdateCannonBalls method that will move the cannonballs across the screen when fired.

Step 4

Watch Video
Create a FireCannonBall method that will use the rotation of the cannon to fire a cannonball at the proper position and velocity.

Note: See the update for this step to fix a known problem.
More Details

Step 5

Watch Video
Modify the UpdateCannonBalls method to "kill" cannonballs that go outside of the screen bounds, to allow the player to fire more.

Step 6

Watch Video
Draw the cannonballs on screen inside the Draw call, so the player can see them when they are fired.
Download source code
var gDomain='m.webtrends.com'; var gDcsId='dcschd84w10000w4lw9hcqmsz_8n3x'; var gTrackEvents=1; var gFpc='WT_FPC'; /*<\/scr"+"ipt>");} /*]]>*/
DCSIMG