Ion101

to make something shoot would this work:

Vector2 bullet

Vector2 player

Bullet = Player;

if(GamePad.GetState(PlayerIndex.One).Button.A == ButtonState.Pressed)

Bullet.X += 5;

if not could you post some code taht would



Re: XNA Game Studio Express Shooting

Richard Kain

Yes, and no. You have the basic idea down, but your structure is somewhat lacking. For instance, your code for updating the bullet's position would only run once as it stands now. So your bullet would travel 5 coordinates to the right, and then it would stop. Setting the bullet position equal to the player position is the correct way to place the bullet initially. But you need the bullet to update constantly, not just once.

A better way to handle this would be to make the bullet an object, and the player an object. Give the player object a position property that will store its current coordinates. Put the code for creating the bullet inside the player object. Make the bullet object's constuctor method take two float variables. When you call the bullet object in the player object, pass it the player object's current position, and use this to create the bullet object with the same position. Then create an UpdateBullet method within the bullet object. Put the code to change the bullet's position in this method. Then call the UpdateBullet method in whatever Update method you make for the player object. Now go back to your Game1 file, and create a player object in the Initialize method. Then call the PlayerUpdate method in the Game1.Update method, and the PlayerDraw method in the Game1.Draw method. Now you have an object-oriented player who will shoot bullets that continue to travel.

Also, in order to save on processing power, it would be a good idea to create a BulletManager. This would most likely be an object or method that would create a set number of bullets, and handle passing them to the player object. That way you won't keep creating and destroying bullet objects. For instance, when a bullet object is fired, you don't create it, you just have it become visible, and start moving. When its traveled a certain distance (aka, when you are sure its off the screen) you just make it invisible, and reset its position to the player's current position. Basically, you would just keep recycling the same bullet objects over and over again. With most games, how fast the player is able to fire determine's how many player bullets can be on the screen at any given time. Make your maximum number of bullets three or four more than this number, just in case.