1
我正在開發一款遊戲,並且在init方法中,當我做[self loadGravityCenters]
時,它會給出EXC_BAD_ACCESS。下面是該方法的定義:爲什麼分配給這個NSMutableArray給EXC_BAD_ACCESS?我會出界嗎?
-(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]];
}
}
總結:
讓intgers陣列存儲的座標。轉換它,以便它可以放置在一個CGPoint數組中。製作一堆CCSprites並給它們從CGPoint填充的數組中獲得座標。
文件我使用看起來像這樣:
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) {
想知道它只是一個越界-事情。但做我以上所做的事似乎並沒有改變任何事情。任何幫助將不勝感激。
嗯,這算出EXC_BAD_ACCESS,我的精靈不顯示......但首先是第一件事。謝謝! – Dair
你確定你的精靈分配得當嗎?此外,重力井和重力中心是否正確分配? – msgambel
我相信,我正在爲.h文件中的所有數組執行'@property(nonatomic,retain)'和'@ synthesize'。 – Dair