简体   繁体   中英

Need help looping through text file in Objective-C instead of looping through multidimensional array to display CCSprites

I have a question regarding an iPhone game I'm developing. At the moment, below is the code I'm using to currently I loop through my multidimensional array and position bricks accordingly on my scene. Instead of having multiple two dimensional arrays within my code as per the following (gameLevel1).

Ideally, I'd like to read from a text file within my project and loop through the values in that instead.

Please take into account that I'd like to have more than one level within my game (possibly 20) so my text file would have to have some sort of separator line item to determine what level I want to render.

I was then thinking of having some sort of method that I call and that method would take the level number I'm interested in rendering.

eg Method to call level based on separator?

-(void)renderLevel:(NSString *)levelNumber;

Method usage:

[self renderLevel:@"#LEVEL_ONE"];

eg Text file example?

#LEVEL_ONE#
0,0,0,0,0,0,0,0,0
0,1,1,1,1,1,1,1,0
0,1,1,1,1,1,1,1,0
0,1,1,1,1,1,1,1,0
0,1,1,1,1,1,1,1,0
0,1,1,1,1,1,1,1,0
0,1,1,1,1,1,1,1,0
0,1,1,1,1,1,1,1,0
0,1,1,1,1,1,1,1,0
0,1,1,1,1,1,1,1,0
0,1,1,1,1,1,1,1,0
0,1,1,1,1,1,1,1,0
0,1,1,1,1,1,1,1,0
0,1,1,1,1,1,1,1,0
0,1,1,1,1,1,1,1,0
0,1,1,1,1,1,1,1,0
0,0,0,0,0,0,0,0,0
#LEVEL_TWO#
1,0,0,0,0,0,0,0,1
1,1,1,1,1,1,1,1,1
1,1,1,1,1,1,1,1,1
1,1,1,1,1,1,1,1,1
1,1,1,1,1,1,1,1,1
0,1,1,1,1,1,1,1,0
1,1,1,1,1,1,1,1,1
0,1,1,1,1,1,1,1,0
1,1,1,1,1,1,1,1,1
0,1,1,1,1,1,1,1,0
1,1,1,1,1,1,1,1,1
0,1,1,1,1,1,1,1,0
1,1,1,1,1,1,1,1,1
1,1,1,1,1,1,1,1,1
1,1,1,1,1,1,1,1,1
1,1,1,1,1,1,1,1,1
1,0,0,0,0,0,0,0,1

Code that I'm currently using:

int gameLevel[17][9] = {
  { 0,0,0,0,0,0,0,0,0 },
  { 0,1,1,1,1,1,1,1,0 },
  { 0,1,1,1,1,1,1,1,0 },
  { 0,1,1,1,1,1,1,1,0 },
  { 0,1,1,1,1,1,1,1,0 },
  { 0,1,1,1,1,1,1,1,0 },
  { 0,1,1,1,1,1,1,1,0 },
  { 0,1,1,1,1,1,1,1,0 },
  { 0,1,1,1,1,1,1,1,0 },
  { 0,1,1,1,1,1,1,1,0 },
  { 0,1,1,1,1,1,1,1,0 },
  { 0,1,1,1,1,1,1,1,0 },
  { 0,1,1,1,1,1,1,1,0 },
  { 0,1,1,1,1,1,1,1,0 },
  { 0,1,1,1,1,1,1,1,0 },
  { 0,1,1,1,1,1,1,1,0 },
  { 0,0,0,0,0,0,0,0,0 }
};

for (int row=0; row < 17; row++)
{
  for (int col=0; col < 9; col++)
  {
    thisBrickValue = gameLevel[row][col];
    xOffset = 35 * floor(col);
    yOffset = 22 * floor(row);

    switch (thisBrickValue)
    {
      case 0: brick = [[CCSprite spriteWithFile:@"block0.png"] autorelease]; break;
      case 1: brick = [[CCSprite spriteWithFile:@"block1.png"] autorelease]; break;
    }
    brick.position = ccp(xOffset, yOffset);
    [self addChild:brick];
  }
}

Having each level in a separate file would make loading faster, especially for later levels, would offer more flexibility, and would probably be a tad easier to implement. These points are merely academical for the number of levels you plan on using.

I am not really sure what your question is, though. I see some question marks, but they don't seem to end questions. Would you mind updating your question with an actual question?

It looks like you want to be parsing a file, which is answered for Objective-C in this question: How do I parse a text file in Objective-C?

For your case, I think you'd want to determine whether each line is a "#Level X#" line or a row of blocks. Then you could use the same componentsSeparatedByString function (passing @"," instead of @"\\n") to give you an array of all the blocks in a line to add to an NSMutableArray of your own creation.

As a personal design choice, I'd rather have each level in a separate file. That allows future addition/update of levels without having to update the file containing all existing levels.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM