这是我的代码
-(NSMutableDictionary*)checkArrayForObject:(NSArray *)array withX:(int)mapX withY:(int)mapY withWith:(int)w withHeight:(int)h
{
NSMutableDictionary *tempDictionary = [[NSMutableDictionary alloc] init];
int cellWidth2 = w;
int cellHeight2 = h;
NSMutableArray *temp = [[NSMutableArray alloc ] initWithCapacity:(cellWidth2*cellHeight2)];
//lets fill the array with something
for (int iy=0; iy<cellWidth2; iy++) {
for (int ix=0; ix<cellHeight2; ix++) {
[temp addObject:[NSNull null]];
}
}
Boolean overObjectYES = YES;
Boolean overObjectNo = NO;
[tempDictionary setObject:[NSNumber numberWithBool:overObjectNo] forKey:@"collision"];
[tempDictionary setObject:temp forKey:@"regionArray"];
NSLog(@"mapx, %d", mapX);
NSLog(@"mapy, %d", mapY);
NSLog(@"cell width, %d", cellWidth2);
NSLog(@"cell height, %d", cellHeight2);
int xx = 0;
int yy = 0;
NSLog(@"DO CHECK");
for (int iy=mapY; iy<(mapY + cellHeight2); iy++) {
xx = 0;
for (int ix=mapX; ix<(mapX + cellWidth2); ix++) {
//make index
int ii = (iy*mapWidth)+ix;
NSLog(@"array index, %d", ii);
if([array objectAtIndex:ii] != [NSNull null])
{
NSLog(@"OVERLAPPING OBJECT");
[tempDictionary setObject:[NSNumber numberWithBool:overObjectYES] forKey:@"collision"];
//get object in real array
NSMutableDictionary *obj = [array objectAtIndex:ii];
//place in temp in same relative position
//make index
int ii2 = (yy*cellWidth2)+xx;
if(ii2 < [temp count])
{
[temp insertObject:obj atIndex:ii2];
}
}
xx++;
}
yy++;
}
return tempDictionary;
}
在另一种方法中我调用此方法,但后来我试图访问已存储在数组中的对象,但我似乎无法访问它,或者上面的代码(insertObject: obj line)似乎没有插入任何东西。
这是我用来访问上面代码的代码......
NSMutableDictionary *regionDictionary = [sharedInstance checkArrayForObject:sharedInstance.playerObjects withX:sharedInstance.mapTileX withY:sharedInstance.mapTileY withWith:1 withHeight:1];
Boolean overlap = [[regionDictionary objectForKey:@"collision"] boolValue];
if(overlap == YES)
{
[self setupInfoMsg:[[regionDictionary objectForKey:@"region"]objectAtIndex:0]];
}
当我单步执行该代码并进入setupInfoMSg时,参数= 0。
我是Obj-c的新手,所以这可能是一个简单的错误。