Shooting multiple bullets

Started by
3 comments, last by zoogy1983 9 years, 10 months ago

Hello,

Below is my bullet class and what I am trying to accomplish is putting bullets in a list so I can shoot multiple bullets. I am having a really hard time trying to figure this out. When my code is running I hit the spacebar and only one bullet shoots. Can someone please let me know what I am doing wrong or at least point me in the right direction, thanks!

[source]using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace OptimusOrbis.GameObjects
{
public class DefaultBullet : WeaponsManager
{
public Ship Ship { get; set; }
public DefaultBullet defaultBullet { get; set; }
List bullets;
KeyboardState mPreviousKeyboardState;

public DefaultBullet()
: base()
{

Ship = new Ship();
LoadImage("bullet");
rateOfFire = 1;
Damage = 1;
distance = 0;
maxDistance = 500;
bullets = new List();

//bullet will be the same position as the ship
//Ship always stays in the middle so does the bullet
//until it is fired off
Position = Ship.Position;
}

new public void Update()
{
base.Update();

//Allows to use the keyboard
KeyboardState aCurrentKeyboardState = Keyboard.GetState();
mPreviousKeyboardState = aCurrentKeyboardState;

Position += direction * speed * (float)gameManager.GameSpeed;

if (aCurrentKeyboardState.IsKeyDown(Keys.Space))
{
bullets.Add(Position);
for (int i = 0; i < bullets.Count; i++)
{
ShootBullet();
}

}
}

///

/// Shoots the default bullets
///
public void ShootBullet()
{
for (int i = 0; i < bullets.Count; i++)
{
Visible = true;
speed = 10.0f;
direction.X = 1;
}

}

new public void Draw()
{
if (Visible == true)
{
for (int i = 0; i < bullets.Count; i++)
{
base.Draw();
}
}
}

}
}
[/source]

Advertisement

You probably have lots of bullets, they're just all at the same position. I don't see how they would ever be at different positions. You appear to only have one instance of a Position object.

I'm assuming that (poorly-named) DefaultBullet is supposed to be some kind of manager for bullets. Is that correct?

There are so many things wrong with this code:

- DefaultBullet creates ship? What?

- If the key is down, you iterate through all the bullets and call ShootBullet. What does ShootBullet do? It iterates through all the bullets again (so now you have an O(n^2) operation). Not only that, but it doesn't really do anything. It never modifies any bullet's position. It just sets the same variables in DefaultBullet manager over and over again.

- You call base.Draw once for each item in the bullets list. What??

Thank you for your response.

A bullet always shoots from the middle of the screen - where the ship is

To answer your question:

This class is actually inherited from a weapons manager class (I changed the name of the class to better represent the kind of weapon I am making)

I am not creating a new ship I am giving the bullets start position where the ship is.

Shoot bullets makes the bullet visible gives it speed and also gives the direction of the bullet.

base.Draw calls the inherrited classes draw method which hought was good since it works just fine.

I know that my probem is in where I press the space key because I need it to update the bullet in the same location and give it the same movement.


This class is actually inherited from a weapons manager class (I changed the name of the class to better represent the kind of weapon I am making)

Why would a class that manages bullets inherit from a weapon manager class? That doesn't make any sense. Weapons create bullets, but they should be totally separate. Maybe your naming is just weird, but conceptually this makes no sense to me.


I am not creating a new ship I am giving the bullets start position where the ship is.

So you create a new Ship object, and then use its position for the start point of bullets? So you're assuming that a Ship object always has the same position? I'm guessing the ship is in the middle of the screen and never moves, so this works. But it's a flawed way to do things. Instead, pass the *actual* Ship instance into your class's constructor and store a reference to it, and use the position of that.


Shoot bullets makes the bullet visible gives it speed and also gives the direction of the bullet.

Hmm, did you not post all your code? ShootBullet just appears to set the Visible property of your class to true. It doesn't seem to do anything with individual bullets. Look at this code:


        public void ShootBullet()
        {
            for (int i = 0; i < bullets.Count; i++)
            {
                Visible = true;
                speed = 10.0f;
                direction.X = 1;
            }
        }

This would accomplish the same thing, wouldn't it?


        public void ShootBullet()
        {
            Visible = true;
            speed = 10.0f;
            direction.X = 1;
        }

I mean, what is this for?


    List bullets;

All you ever do is add positions to this list. It doesn't look like you ever DO anything with those positions.

Why would a class that manages bullets inherit from a weapon manager class? That doesn't make any sense. Weapons create bullets, but they should be totally separate. Maybe your naming is just weird, but conceptually this makes no sense to me.

You are correct, my naming convention is weird, but perhaps I should have a bullet manager as well. Which in reallity I can rework this class into that. I will look into this. Thanks!

My ship is always in the middle of the screen by design. I know that I can give it the ships position at all times and I will although the actual ship never moves.

As far as the movement of my bullet it is in the Update method:

[source] Position += direction * speed * (float)gameManager.GameSpeed;[/source]

I have the variables in the ShootBullet in a for loop hoping that it would go through my list of bullets and do that for every bullet. So I guess my big problem is getting the bullets into a list. Like I said I am able to shoot one bullet but getting the bullets in the list and shoot every time the button is pressed is where I am having the issue.

Thanks

This topic is closed to new replies.

Advertisement