需要帮助在 Objective-C 中循环文本文件而不是循环多维数组来显示 CCSprites
我对我正在开发的 iPhone 游戏有疑问。目前,下面是我当前使用的代码,用于循环遍历多维数组并在场景中相应地定位砖块。而不是按照以下方式在我的代码中包含多个二维数组(gameLevel1)。
理想情况下,我想从项目中的文本文件中读取并循环其中的值。
请考虑到我希望在游戏中拥有多个关卡(可能是 20 个),因此我的文本文件必须具有某种分隔行项目来确定我想要渲染的关卡。
然后我正在考虑使用某种我调用的方法,该方法将采用我感兴趣的渲染级别号。
例如根据分隔符调用级别的方法?
-(void)renderLevel:(NSString *)levelNumber;
方法用法:
[self renderLevel:@"#LEVEL_ONE"];
例如文本文件示例?
#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
我当前正在使用的代码:
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];
}
}
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;
Method usage:
[self renderLevel:@"#LEVEL_ONE"];
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];
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将每个级别放在单独的文件中将使加载速度更快,特别是对于后面的级别,将提供更大的灵活性,并且可能更容易实现。这些要点只是针对您计划使用的级别数量的学术观点。
不过,我不太确定你的问题是什么。我看到一些问号,但它们似乎并没有结束问题。您介意用实际问题更新您的问题吗?
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?
看起来您想要解析一个文件,这是 Objective-C 在这个问题中的答案: 如何在 Objective-C 中解析文本文件?
对于你的情况,我认为你想要确定每一行是“#Level X#”行还是一行块。然后,您可以使用相同的 ComponentsSeparatedByString 函数(传递 @"," 而不是 @"\n")来为您提供一行中所有块的数组,以添加到您自己创建的 NSMutableArray 中。
作为个人设计选择,我宁愿将每个级别放在单独的文件中。这允许将来添加/更新级别,而无需更新包含所有现有级别的文件。
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.