• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
zoogy1983

Shooting multiple bullets

4 posts in this topic

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]

 

 

0

Share this post


Link to post
Share on other sites

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??

2

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites


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.

0

Share this post


Link to post
Share on other sites

 

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

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0