-1
\$\begingroup\$

I'm not understanding how to dynamically add object instances in my games.

I'm really having trouble understanding the general concept. If I want to quickly generate unknown numbers of objects, going through and declaring them one by one does not seem to be plausible. This problem is exemplified by bullets. If the player can fire 100 bullets, how do I declare and define these objects in a simple manner. Obviously I need formulas to tell the bullets where to appear and where to go, but I can do that. How do I get to the point where I can begin to define those formulas?

Here is the code up to the point I have tried, the current problem being that B cannot be assigned because it is the essence of the foreach loop:

if (ms.LeftButton == ButtonState.Pressed)
{
    shotFired = 0;
    foreach (Bullet B in bulList)
    {
        if(B != null)
        {
            B = new Bullet(main.Location, enball, spriteBatch);
            shotFired = 1;
        }
     }
    //if all bullets are not "null," essentially delete one and replace it.
}
\$\endgroup\$
3
  • 1
    \$\begingroup\$ Considering the questions you've asked, you may want to brush up on some coding before getting too deep into more game development. Self taught programming is all good, but you might want to go through some online courses just to solidify what you know already. I think it'll help you avoid a lot of issues. \$\endgroup\$
    – House
    Commented Jan 5, 2014 at 19:04
  • \$\begingroup\$ @Byte56 I have a somewhat good knowledge of programming, having developed web apps. I'm just having trouble applying it to game development. I was looking for a tutorial that would guide me through all of this last night, but failed to find one. All the tutorials I can find make simplistic games with little need for automated checks on hundreds of enemies/bullets. Do you know of any particularly good ones? \$\endgroup\$
    – James G.
    Commented Jan 5, 2014 at 21:46
  • \$\begingroup\$ I don't typically do tutorials myself, I don't know of any good ones. The reason I mentioned it was because your questions appeared to be mostly about misunderstanding of syntax or how some basic code structures work. For example, your code sample above shows a lack up understanding for how loops work (since it does not do what you describe in your comment, it actually replaces all the bullets in your list). \$\endgroup\$
    – House
    Commented Jan 6, 2014 at 1:24

2 Answers 2

4
\$\begingroup\$

To fire a bullet every time the mouse is clicked, with no limit to the number of bullets that can exist, simply do:

if (ms.LeftButton == ButtonState.Pressed) {
  var bullet = new Bullet(main.Location, enball, spriteBatch);
  bulList.Add(bullet);
}

If you want to have a fixed number of bullets (represented by bulList) and use the first null entry in the list you find to hold the newly-fired bullet, use a for loop instead (because as you noted, you cannot modify the foreach enumeration variable in that fashion):

if (ms.LeftButton == ButtonState.Pressed) {
  for (var index = 0; index < bulList.Count; ++index) {
    if (bulList[index] == null) {
      bulList[index] = new Bullet(main.Location, enball, spriteBatch);
      break;
    }
  }
}

You can also implement that "if no bullets were null, re-use an existing one" logic in the latter example if you like.

Note that a better way to handle the above is to instead partition your list and keep a "dead index" such that every live bullet is below the dead index and every dead (null) bullet is above it. This can help you avoid the costly linear search through the bullet array every time you fire.

\$\endgroup\$
4
  • \$\begingroup\$ Your first bit of code overwrites the old bullet when the new one appears? \$\endgroup\$
    – James G.
    Commented Jan 5, 2014 at 6:35
  • \$\begingroup\$ The first loop adds new bullets to the end of the list. \$\endgroup\$
    – user1430
    Commented Jan 5, 2014 at 16:26
  • \$\begingroup\$ Yes but the first line of code var bullet = new Bullet(main.Location, enball, spriteBatch); erases the old bullet. Like if x = 1; then the next line x=2;, the old x is "erased" . \$\endgroup\$
    – James G.
    Commented Jan 5, 2014 at 17:31
  • 1
    \$\begingroup\$ No, it doesn't. It creates a completely new Bullet object, stored in a local variable (bullet) which goes out of scope at the end of the block (after storing the bullet reference in the list). Unless your Bullet class has static state modified by the constructor that you have not shown, this new instance is totally independent. \$\endgroup\$
    – user1430
    Commented Jan 5, 2014 at 19:00
0
\$\begingroup\$

This is a very simple question.
When you get the click event, add a new bullet into your bullet array. Then, you can update and draw the bullets at their positions every frame. When you update bullets, you can make them go forward, and check their collisions with objects. Then just draw them.
It is not as hard as you think.
Here are some links:
http://en.wikibooks.org/wiki/Creating_a_Simple_3D_Game_with_XNA
http://www.riemers.net/

\$\endgroup\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.