Ion101

What code could u use for something to shoot. would this event work:

if(GamePad.GetState(PlayerIndex.one).Buttons.A == ButtonState.pressed)
shoot = true;

if(Shoot = True)
{
bullet.X = Player.X;
bullet.Y = Player.Y;

buller.Y += bulletSpeed;
shoot = false;
}


Re: XNA Game Studio Express Shooting

atlStylez

An easier way, that would be much more scalable is to possibly use a list of bullets, that contain bullets that have been fire.

Check the input of the player, if the button to shoot is pressed, create a new bullet, and add it to the list of bullets. And every frame, you update all the bullets.

struct myBullet
{
int x, y;
int speed;
int damage;
}

List<myBullet> _Bullets;

public void CreateBullet()
{
myBullet bull = new myBullet();
bull.X = player.X;
bull.Y = player.Y;
bull.speed = 100;
bull.damage = 5;
_Bullets.add(bull);
}


public void Update(float Elapsed)
{
if(GamePad.GetState(PlayerIndex.one).Buttons.A == ButtonState.pressed)
CreateBullet();


for(int i = 0; i < _Bullets.Count; i++)
{
_BulletsIdea.y += (speed * Elapsed);
// then check to see if the bullet has hit anything, or has gone off the scree

}
}

That's very QUICK, DIRTY, and messy way, but that's the general idea. You should check out PlanetSourceCode.com, in the VB section, there are a lot of great top down shooter game examples.





Re: XNA Game Studio Express Shooting

B1narySh0t

Hey Ion101, you have the basic idea, but there is a problem. In your Update method, the bullet will not continuosly move across the screen. It will first be set to the position of the player, and then it will be moved downwards (if you are drawing in 2D) by bulletSpeed amount.

What you want to do, is setup another component for the bullet class.

I would suggest a class like the following:

class Bullet
{
Vector2 _position;
Vector2 _velocity;

public Vector2 Velocity { set { _velocity = value; } }

public Bullet( Vector2 initialPosition, Vector2 initialVelocity)
{
_position = initialPosition;
_velocity = initialVelocity;
}

public Update(GameTime gameTime)
{
_position += (float)_velocity*gameTime.ElapsedGameTime.Milliseconds/1000.0f;
}
}

Then in your Update method, do something like the following:

if (Shoot == True)
{
bullet = new Bullet(player.Position, new Vector2(0, bulletSpeed);
bulletList.Add(bullet);
}

//Update all bullets in the bullet list
foreach (Bullet bullet in bulletList)
{
bullet.Update(gameTime);
}

This way, every bullet you have will then be set with some velocity and will move on its own a particular amount (set by you) everytime the Update method is called.

In your Game class, you want to have a structure to hold all the bullets, something like:

private List<Bullet> bulletList = new List<Bullet>();

From this you can see that the bullet will continuously move without checking whether you are shooting and whatnot. This is a basic example of how you can achieve what you are asking, but it demonstates the techniques.

Hope it helps.





Re: XNA Game Studio Express Shooting

Ion101

i got both of u so thanx. but in this,

 List<myBullet> _Bullets 

ia that calling this

 struct myBullet
{
int x, y;
int speed;
int damage;
}

then what is the _Bullets is it the texture or something





Re: XNA Game Studio Express Shooting

Ion101

So will this draw the bullet at my sprite, so it looks like he is shooting.  Or do i have to add something to my draw method. also in this

bullet = new Bullet(player.Position, new Vector2(0, bulletSpeed);

where is the bulletSpeed variable declared.





Re: XNA Game Studio Express Shooting

B1narySh0t

Yes, this will initially draw the bullet at your sprite. You only want to draw the bullet at it's recorded position.

The position of the bullet will change each time you call Update() on it, thus making it move across the screen.

As for the bulletSpeed, it is just some value you can use to set the velocity of the bullet.

In an example, say you want the bullet to move 128 units / second.

Well, since the update performs time based movement, setting bulletSpeed to 128 will achieve this.





Re: XNA Game Studio Express Shooting

Ion101

whenever i hit A to fire, it doesn't draw my bullet to the screen and says Sytem.NullRefence to

 bull = new bullet(position, new Vector2(0, bull.bulletSpeed));
                  bulletList.Add(bull);

do i need to add something to the draw method




Re: XNA Game Studio Express Shooting

DanBrink

You shouldn't be trying to move both the x and y with that, if the bullet is moving down...

bullet.y -= (float)_velocity*gameTime.ElapsedGameTime.Milliseconds/1000.0f;
Vector2 _position = new Vector2(bullet.x,bullet. y);

That's what I do anyway...




Re: XNA Game Studio Express Shooting

B1narySh0t

I was just demonstrating a way of doing it, if you just wanted to move the bullet down, then set the bulletSpeed as a Vector2 and set the X component to 0.0f.

Why continously recreate a Vector2 when all you need to do is update the _position vector that the class holds.

Also, one thing I may have forgot to mention, when dealing with vectors, if you want them to apply a velocity of say 128 units/second, then make sure you normalize your vectors.

Vector2 newVelocity = _velocity;
newVelocity.Normalize();

This will make the vector a unit vector, and then you can multiply the new velocity vector by a unit speed say 128. This will make your sprite move in any direction 128 units/second.

EDIT:   I am not sure what is causing your Null pointer exception, make sure that you have constructed all your objects with the new operator.

Like
List<Bullet> _bullets = new List<Bullet>();
Bullet bullet = new Bullet( ...blah ...);

Then you can add the bullet to the list.

Check which statement is giving you the error, if you are still having problems, post the code region that is causing the exception and we can probably help you out.




Re: XNA Game Studio Express Shooting

Ion101

here is the bullet class

public class Bullet
{
Vector2 _position;
Vector2 _velocity;
public int bulletSpeed = 10;


public Vector2 Velocity { set { _velocity = value; } }

public Bullet( Vector2 initialPosition, Vector2 initialVelocity)
{
_position = initialPosition;
_velocity = initialVelocity;
}

public void Update(GameTime gameTime)
{
_position += _velocity*gameTime.ElapsedGameTime.Milliseconds/1000.0f;
}
}

here is my main variable declarations in the Game1.cs

Player player;
Bullet bullet;
List<Bullet> bulletList = new List<Bullet>();
bool shoot = false;

here is the update method where the null keeps coming

protected override void Update(GameTime gameTime)
{
// Allows the default game to exit on Xbox 360 and Windows
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();

// TODO: Add your update logic here

if (GamePad.GetState(PlayerIndex.One).Buttons.A == ButtonState.Pressed)
shoot = true;
if (shoot == true)
{
bullet = new Bullet(player.position, new Vector2(0, bullet.bulletSpeed));
bulletList.Add(bullet);
}
foreach (Bullet bull in bulletList)
bull.Update(gameTime);

base.Update(gameTime);
}




Re: XNA Game Studio Express Shooting

B1narySh0t

Ion101 wrote:
here is the bullet class

public class Bullet
{
Vector2 _position;
Vector2 _velocity;
public int bulletSpeed = 10;


public Vector2 Velocity { set { _velocity = value; } }

public Bullet( Vector2 initialPosition, Vector2 initialVelocity)
{
_position = initialPosition;
_velocity = initialVelocity;
}

public void Update(GameTime gameTime)
{
_position += _velocity*gameTime.ElapsedGameTime.Milliseconds/1000.0f;
}
}

here is my main variable declarations in the Game1.cs

Player player;
Bullet bullet;
List<Bullet> bulletList = new List<Bullet>();
bool shoot = false;

here is the update method where the null keeps coming

protected override void Update(GameTime gameTime)
{
// Allows the default game to exit on Xbox 360 and Windows
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();

// TODO: Add your update logic here

if (GamePad.GetState(PlayerIndex.One).Buttons.A == ButtonState.Pressed)
shoot = true;
if (shoot == true)
{
bullet = new Bullet(player.position, new Vector2(0, bullet.bulletSpeed));
bulletList.Add(bullet);
}
foreach (Bullet bull in bulletList)
bull.Update(gameTime);

base.Update(gameTime);
}

Ok I see a problem, take a look at this line of code:

bullet = new Bullet(player.position, new Vector2(0, bullet.bulletSpeed));

From what I can see, the offending statement is trying to use bullet.bulletSpeed without first instantiating the bullet object. I would either make this a static constant in some static class, or enumerated type, or a constant in your main game class, the choice is yours (there are many more, these are only a few options).

Basically, the method you have chosen requires you have to create an instance of bullet before you can use it's field. Moving this value outside of the class will remove this restriction.

Also, I hope that you have instantiated your player class before using it's position vector as well





Re: XNA Game Studio Express Shooting

Neverwhere

Additionally, you are using
public void Update(GameTime gameTime)
{
_position += _velocity*gameTime.ElapsedGameTime.Milliseconds/1000.0f;
}

But you are dividing Milliseconds by 1000, which essentially turns Milliseconds into Microseconds. I'm not 100% (more like 99.99%) sure that this isn't entirely what you want, but if you don't actually want that (again, I'm guessing you don't) your bullet might actually be moving, albeit very, very slowly. Try not dividing Milliseconds by 1000, or changing ElapsedGameTime.Milliseconds to ElapsedGameTime.Seconds





Re: XNA Game Studio Express Shooting

Ion101

could u show me exactly what u mean by static constant or enumerated type.
also du u have to render the bullet texture in the loadgraphics method
and yes i have intitiad my player class




Re: XNA Game Studio Express Shooting

B1narySh0t

If you look closely, Milliseconds returns an integer value in the total number of Milliseconds elapsed since the last call to Update(). By dividing by 1000.0f I am effectively turning the integer result into a floating point value representing the total Milliseconds elapsed.

e.g. If milliseconds returns 16, which means 16 milliseconds elapsed since the last call, then dividing by 1000.0f gives me: 0.016 seconds. To me, this is more accuracte then using Seconds. But this is all preference, it's just the way I have been doing time based movement in all my games prior to XNA.

So, if I want an object to move 128 units/second, then if 16 milliseconds have elapsed, my object will only move 2.048 units.

I think Seconds also returns an integer result as well, so if only 16 milliseconds have elapsed, Seconds will only return 0. This is not useful when doing time based movement. Correct me if I am wrong.





Re: XNA Game Studio Express Shooting

B1narySh0t

Ok, for a static class, you could do one of the following:

public static class Constants
{
public static int BULLET_SPEED = 128;
...
}

Ok, now lets say you are firing the bullet at vector < 1, 1>. Lets say this is held in the variable bulletVelocity;

So,

bulletVelocity = new Vector2(1,1);

In order to move the bullet at constant speed regardless of direction, normalize the vector.

bulletVelocity.Normalize();

Then when you call your bullet instantiation code, do the following:

bullet = new Bullet( _player.position, bulletVelocity * Constants.BULLET_SPEED );

Simple, tidy, and you can adjust the bullet speed any time, and it only needs to be done in one location.