Я работаю над игрой, и в моем методе init она выдает EXC_BAD_ACCESS, когда я делаю [self loadGravityCenters]
.Вот определение этого метода:
-(void) loadGravityCenters {
//These P1, P2, etc. are for seeing where the EXC_BAD_ACCESS is in the method.
printf("P1\n");
//Get and open the file with coordinates
NSString *filePath = [[NSBundle mainBundle] pathForResource: @"gravitycenter" ofType: @"ballpoint"];
FILE *fileHandle = fopen([filePath cStringUsingEncoding:NSASCIIStringEncoding],"r");
//First array for storing the coordinates from the file.
NSMutableArray *coord;
//Vanilla C doesn't have an NSMutableArray, use tempCoord to place it in the NSMutableArray.
int tempCoord;
//i is used to keep track of where to assign stuff in coord).
int i = 0;
while (!feof(fileHandle)) { //Read file.
//Next check point.
printf("P2\n");
//Assign tempCoord a number from file.
fscanf(fileHandle, "%d", &tempCoord);
//Convert the integer to NSNumber (so I can put it in coord.
NSNumber *intToNSNumber = [NSNumber numberWithInt:tempCoord];
[coord insertObject:intToNSNumber atIndex:i];
i++;
}
for (int j = 0; j < [coord count]; j+=2) { //Make the coord array into an array of CGPoints
printf("P3\n"); //Check point.
//Assign a gravityCenters (declared in header) a series of points.
[gravityCenters addObject:[NSValue valueWithCGPoint:CGPointMake([[coord objectAtIndex:j] floatValue], [[coord objectAtIndex:j+1] floatValue])]];
}
for (int j = 0; j < [gravityCenters count]; j++) {
printf("P4\n"); //Last checkpoint.
//gravityWells (declared in header) is used to store CCSprites, make a bunch of sprites with the positions stored in gravityCenters.
[gravityWells addObject:[CCSprite spriteWithFile:@"GravityCenter.png"]];
[[gravityWells objectAtIndex:j] setPosition:[[gravityCenters objectAtIndex:j] CGPointValue]];
printf("gravityWell=%f,%f\n", ((CCSprite *)[gravityWells objectAtIndex:j]).position.x, ((CCSprite *)[gravityWells objectAtIndex:j]).position.y);
[self addChild:[gravityWells objectAtIndex:j]];
}
}
Подведем итог:
Создайте массив целых чисел, в котором хранятся координаты.Преобразуйте его, чтобы он мог быть помещен в массив CGPoints.Создайте набор CCSprites и дайте им координаты из массива, заполненного CGPoints.
Файл, который я использую, выглядит следующим образом:
70 100
80 89
46 545
Сбой программы на контрольной точке P3.Я попытался заменить строку:
for (int j = 0; j < [gravityCenters count]; j++) {
на:
for (int j = 0; j < [coord count]-2; j+=2) {
//And:
for (int j = 0; j < [coord count]-3; j+=2) {
Интересно, это только за гранью.Но то, что я сделал выше, ничего не меняет.Любая помощь будет принята с благодарностью.