0

I have a class "Bullet" which I instantiate using a method CreateBullet(), since there are going to be multiple bullets i decided that I should make bullet an array, though this didn't work out and I've spent an hour on trying to fix it.

What I call in my Initialize method:

Bullet bullet[] = Bullet.CreateBullet[1]();

The Bullet class:

class Bullet
{
    public float2 position;
    public float angle { get; set; }
    public float speed { get; set; }
    public static Bullet CreateBullet()
    {
        Bullet bullet = new Bullet()
        {
            position = new float2()
        };
        return bullet;
    }
    public void Move()
    { 
    }
}

Could you please show me what's wrong with the code? Thank you in advance.

6
  • 1
    Making a game before you learn how to populate a simplest array? This is wrong: Bullet bullet[] = Bullet.CreateBullet[1]();, among other things... Start reading the documentation msdn.microsoft.com/en-us/library/aa288453%28v=vs.71%29.aspx Commented Dec 25, 2014 at 0:53
  • 1
    you might want to look into generics: List<Bullet> will be much easier to work with. Commented Dec 25, 2014 at 0:55
  • Sorry I know it seems really stupid but I've had little experience with arrays before. Commented Dec 25, 2014 at 0:57
  • Thank you, I'll try using lists :) Commented Dec 25, 2014 at 0:58
  • You dont necessarily need an array here anyways, as @Plutonix has mentioned a generic list of type Bullet will do a much better job. Have a look at them here> msdn.microsoft.com/en-us/library/512aeb7t.aspx Commented Dec 25, 2014 at 1:01

2 Answers 2

2

With this, you create an array of 5 bullets:

        Bullet[] bullets = new Bullet[5];

And then you need to fill the array by creating a bullet for each array entry:

        for (int i = 0; i < bullets.Length; i++)
        {
            bullets[i] = Bullet.CreateBullet(); 
        }

You can wrap this logic in a function:

    public Bullet[] CreateBullets(int amount)
    {
        Bullet[] bullets = new Bullet[amount];
        for (int i = 0; i < bullets.Length; i++)
        {
            bullets[i] = Bullet.CreateBullet(); 
        }

        return bullets;
    }

And then you can use a function to initialize the array:

    public void Test()
    {
        Bullet[] bullets = CreateBullets(5);
    }
Sign up to request clarification or add additional context in comments.

Comments

0

You could do something like this, not quite what you where trying to achieve, but it might inspire you a bit more

Usage

// Create your bullets
var bullets = new List<Bullet>();

// Create a raw/empty bullet with default properties 
var newBullet1 = new Bullet();

// Create bullet with some initialized properties
var newBullet2 = new Bullet()
   {
      Angle = 35, 
      Position = 0, 
      Speed = 200
   };

bullets.Add(newBullet1);
bullets.Add(newBullet2);

Something extra for fun

// Move all your bullets at once
foreach (var bullet in bullets)
{
   bullet.Move();
}

5 Comments

A class representing a collection should be named that way ("Hey, give me that bullets" sounds stupid), like "BulletCollection" or "BulletList". That said, unless you give it explicit functionality related to bullets, I'd stick with List<Bullet>.
Fair comment, i personally like Bullets though :)
Thank you great and powerful Saruman, your wizardry and code outmatch that of mine!
@Croccy remember to upvote and mark questions accordingly, if and when they suit your purpose :)
@DanielMann ok you learn something new every day, can i have my 2 rep points back now thanks :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.