I am making a game which has a global (static) class called MedGameController. In my class I have two arrays of fifteen objects each which hold the logic for each players开发者_JS百科 units. In the game the player's go to a form where they see their already created units and have buttons to create more units. I don't know how to check if current units exist though so I can't even create a single unit. How do I check if the instance of my unit class exists in the array? The array of units have to have a scope which incompasses two maybe three forms so that is why I created them in a global class. Here is the global class:
static class MedGameController
{
static int ply1pts;
static int ply2pts;
Squad[] ply1squads = new Squad[15];
Squad[] ply2squads = new Squad[15];
public static int SetPly1Pts
{
get { return ply1pts; }
set { ply1pts = value; }
}
public static int SetPly2Pts
{
get { return ply2pts; }
set { ply2pts = value; }
}
public static int SquadSetUp
{
get { return squadcreation; }
set { squadcreation = value; }
}
public static void Player1Squads
{
This is where I think i'm supposed to check if they exist then if it doesn't then I create the instance of the squad class
}
}
}
Try this:
if (ply1Squads.Length == 0)
{
for (int i = 0; i < 15; i++)
{
ply1Squads[i] = new Squad();
}
}
You've already declared ply1Squads
to have 15 Squad
objects, so it's just a matter of initializing the objects at that point.
You can do the same thing for ply2Squads
.
BTW, you need to declare ply1Squads
and ply2Squads
as static
as well, as static classes can't have instance members:
static Squad[] ply1Squads = new Squad[15];
static Squad[] ply2Squads = new Squad[15];
By default, these will be private variables, so you'll want properties for these two arrays as well:
public static Squad[] Ply1Squads
{
get
{
return ply1Squads;
}
set
{
ply1Squads = value;
}
}
public static Squad[] Ply2Squads
{
get
{
return ply2Squads;
}
set
{
ply2Squads = value;
}
}
Additional Thoughts
If your design goal is to have MedGameController
handle all the information related to the squads, I'd move away from what you appear to be doing - creating duplicate methods for each Squad
array (i.e., Player1Squads
). Instead, pass in a flag of some sort telling the controller which array to use, like this:
public void CreateSquads(int playerNum)
{
select (playerNum)
{
case 1:
for (int i = 0; i < 15; i++)
{
ply1Squads[i] = new Squad();
}
break;
case 2:
for (int i = 0; i < 15; i++)
{
ply2Squads[i] = new Squad();
}
break;
default:
// Handle wrong number here
break;
}
}
That's a (very small) refactoring step. I would come up with a list of things you want the controller to do, and then figure out how to avoid a 1-1 mapping of every function to both arrays.
Hopefully this makes some sense and gives you a direction to go in. I have some more ideas tickling the back of my brain, but I can't pull them out to the front yet. If I do, I'll add more if you're interested (what's another edit or two after the first half-dozen, right? :) ).
精彩评论