0

I am working on a game that requires a tile map saved in multidimensional array. In my game I have all of these maps made that are NSStrings with all the saved values needed, I'm looking to save all the 256 values of the NSString into an int 16x16 multidimensional array.

Here is my current code however it doesn't work

-(void)LoadMap:(NSString*)mapString
{
    for(int h = 0; h < kMapSize; h++)
    {
        for(int w = 0; w < kMapSize; w++)
        {
            map[w][h] = [[mapString substringWithRange:NSMakeRange((h)+(w*kMapSize), 1)] intValue];
        }

    }

}

Any help would be great thankyou :)

4

2 回答 2

1

有两个潜在的错误:

  • kMapSize可能不等于 16。这个变量有一个误导性的名称,因为不经意的读者会认为它应该是 256。也许重命名它kMapWidth
  • mapString可能不是 256 个字符长。您可能想[mapString length]LoadMap.
于 2012-08-04T01:49:07.703 回答
1

下面的代码没有问题。也许第二行是错误的原因。

// initial data
NSInteger kMapSize = 3;
char map[3][3];   <-- this row ?
NSString *mapString = @"000111222";

// put initial data into 'map' array
for(int h = 0; h < kMapSize; h++) {
    for(int w = 0; w < kMapSize; w++) {
        map[w][h] = [[mapString substringWithRange:NSMakeRange((h)+(w*kMapSize), 1)] intValue];
    }
}

// confirm whether the data is stored successfully
for (int h = 0; h < kMapSize; h++) {
    for (int w = 0; w < kMapSize; w++) {
        NSLog(@"%d", map[w][h]);
    }
}
于 2012-08-04T14:24:26.373 回答