Hi,
I am building a simple game for 2-4 players in Cocoa. This is my first Cocoa app, so not being familiar with the libraries, I am probably doing a lot of weird things. If you see anything that could be done better, just say so.
The first step requires user to choose number of players from NSPopUp and that data is pulled into an int. I create an array and loop in a number of player objects.
- (IBAction)startGame:(id)sender {
// Get value of playersPopup into a string
NSString *playersReturn = [[NSString alloc] initWithString:[playersPopup titleOfSelectedItem]];
// Convert that string to an int
numberOfPlayers = [playersReturn intValue];
// Create an array
playerArray = [[NSMutableArray alloc] init];
// Create a counter
int i = 0;
// While counter is less than number of players, create player object and add to array
while (i < numberOfPlayers) {
// Create a player object
Player *player = [[Player alloc] init];
// Add player to array
[playerArray addObject:player];
// Increment
i++;
}
}
It's a bit weird because all the objects have the same name. I'm not sure how to programmatically give the objects a title using the counter like player1, etc, but its easy enough to pull them by key from the array with objectAtIndex. That would be part 1 of my question: in Cocoa how do I use the counter in naming the objects? Because from what I gather if I did:
player *player[i] =
it would create arrays of that object when all I want to do is call it player1 etc.
Second question is: I have another view where, after the player objects are created, user sees NSTextFields to input names for the players. Given that I should declare the IBOutlets in the AppDelegate, I'm not sure how to provide for a range 2-4 players. I can declare all 4 outlets and just use two, and then just programmatically hide unused textfields. But it seems like there ought to be a way to just create the outlets and view objects based on how many players has been selected. I mean I have that int for the whole AppDelegate so I should use it somehow.
If someone wouldn't mind pointing me a good direction, that would be awesome! I really enjoy programming in Cocoa, but it's kind of a different world at this point. I knew there would be a learning curve, but it's just really different from .net!
Thanks guys!
-Alec