Soapo

I am creating a space shooter game. So far you have a ship that moves, and a Scrolling bg. Exciting Isní»t it.

I used to program in blitz basic and they had a thing called a type that could create multiple objects with multiple similar variables. Like if I were to create 20 alien ships with there own x positions and speed, witch I gave each a random value. Each LV I would add 5 more aliens to the LV until it became impossible. How could I create a type or something similar in C#. Also how can I give a variable a random value.



Re: XNA Game Studio Express A struct / types what ever you call em in C#!

Spyrrho

Random r = new Random();
int i = r.Next(0,100);

If your gonna have to manage alot of items in an array I believe it would be better to use classes as stucts have to be boxed/unboxed when stored in an array. In context of game loop I'd assume this would cause a small hit... though I'm not 100% positive I'm correct here.





Re: XNA Game Studio Express A struct / types what ever you call em in C#!

Soapo

Well thanks for the random value stuff but im not shure if I get what you mean about my Struct / Type problem.



Re: XNA Game Studio Express A struct / types what ever you call em in C#!

Stephen Styrchak - MSFT

I'm not sure if you're asking how to declare a class, or a collection.

There are several kinds of types, but I think the one you want is called a class.

For example (drawing from your description),

internal class AlienSpaceship

{

  private float x;

  private float y;

  private float speed;

  // other stuff here, like properties and methods

}

The above declaration defines a new type called AlienSpaceship, which has three float-type variables. You can instantiate as many as you like.

If you wanted 5 of them, then you could declare an array, like this:

    AlientSpaceship[] ships = new AlienSpaceship[5];

    for (int i = 0; i < ships.Length; ++i)

    {

      ships[ i ] = new AlienSpaceship();

    }

You might want to start with some C# tutorials if this actually answers your question (since you'll also need to know about visibility and a bunch of other stuff). There are also forums specifically for C# questions, which is more appropriate for this sort of thing.

Hope that helps,

--Stephen





Re: XNA Game Studio Express A struct / types what ever you call em in C#!

Jim Perry

Would using a List<AlienSpaceship> be better The ship class could have an IsAlive member that is used to determine whether or not to render and update it. You'd avoid having to new the array every level, you could just reset the existing ships for the next level.