iphone - Creating multiple class instances on the fly -
My question includes a simple game in which 2-4 people can play. This is a game that is played on an iPhone / iPod touch, so no networking is necessary. When the game starts, the user will select the number of players.
For each player, I need to create a player class and frequency. My question is how do I give code, so that only the number of required sections can be made and all of them have been given different names. I had thought about the following, but I know this will not work.
Let's say I have an array named "PlayerName" that stores names of players in the game.
for (int i = 0; i & lt; [count of player names]; i ++) {player * player = [[player illusion] IIT]; // other code}
I can not just keep "i" in the form of a counter for loop and can not get four examples of nominated player 1, player 2 etc. How do i do this
Thank you in advance for your help.
You can not create variable examples on the fly - you need to collect as a result to store the players . A C array or NSMutableArray will work well. You can add this collection to your controller or look for players to make it accessible through the game.
In addition to this, you want to use autorelease
to automatically free up your computer, or alternatively to avoid memory leaks, your Delawk
method items in the collection.
Here are some code that I believe will add you to your class interface (and start somewhere, usually viewDidLoad
)
< Code> - (zero) CreatePlayers: (int) playerCount {for (int i = 0; i <[playerNames count]; i ++) {[players array dot: [[[player alok] iit] autoresaves] Endx: ii]; }}
Comments
Post a Comment