Need help looping through text file in Objective-C and looping through multidimensional array of dat
Posted
by Fulvio
on Stack Overflow
See other posts from Stack Overflow
or by Fulvio
Published on 2010-03-17T05:37:23Z
Indexed on
2010/03/17
22:21 UTC
Read the original article
Hit count: 381
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.
e.g. Method to call level based on separator?
-(void)renderLevel:(NSString) levelNumber;
e.g. 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];
}
}
© Stack Overflow or respective owner