所以这是理解我的问题所需的背景代码。在我的视图控制器类中,我有一个方法可以创建一个名为 levelStart 的数组,然后将该数组发送到我的游戏类,该类使用该数组进行自我初始化,然后我有一组函数使用该数组“玩游戏” . 这是视图控制器代码:
NSMutableArray* levelStart = [[NSMutableArray alloc] init];
NSMutableArray* levelFinish = [[NSMutableArray alloc] init];
NSString* startString = [[NSString alloc] initWithString:@"9999999999988888888998888888899888888889988888888998888888899888888889911888888991188888899999999999"];
NSString* finishString = [[NSString alloc] initWithString:@"9999999999988888811998888881199888888889988888888998888888899888888889988888888998888888899999999999"];
int currentsquare;
for (int i = 0; i < 100; i++) {
currentsquare = [startString characterAtIndex:i] - '0';
if (currentsquare == 1) {
NSNumber* numb1 = [[NSNumber alloc] initWithInt:1];
[levelStart addObject:numb1];
}
if (currentsquare == 2) {
NSNumber* numb2 = [[NSNumber alloc] initWithInt:2];
[levelStart addObject:numb2];
}
if (currentsquare == 3) {
NSNumber* numb3 = [[NSNumber alloc] initWithInt:3];
[levelStart addObject:numb3];
}
if (currentsquare == 4) {
NSNumber* numb4 = [[NSNumber alloc] initWithInt:4];
[levelStart addObject:numb4];
}
if (currentsquare == 8) {
NSNumber* numb8 = [[NSNumber alloc] initWithInt:8];
[levelStart addObject:numb8];
}
if (currentsquare == 9) {
NSNumber* numb9 = [[NSNumber alloc] initWithInt:9];
[levelStart addObject:numb9];
}
}
for (int i = 0; i < 100; i++) {
currentsquare = [finishString characterAtIndex:i] - '0';
if (currentsquare == 1) {
NSNumber* fnumb1 = [[NSNumber alloc] initWithInt:1];
[levelFinish addObject:fnumb1];
}
if (currentsquare == 2) {
NSNumber* fnumb2 = [[NSNumber alloc] initWithInt:2];
[levelFinish addObject:fnumb2];
}
if (currentsquare == 3) {
NSNumber* fnumb3 = [[NSNumber alloc] initWithInt:3];
[levelFinish addObject:fnumb3];
}
if (currentsquare == 4) {
NSNumber* fnumb4 = [[NSNumber alloc] initWithInt:4];
[levelFinish addObject:fnumb4];
}
if (currentsquare == 8) {
NSNumber* fnumb8 = [[NSNumber alloc] initWithInt:8];
[levelFinish addObject:fnumb8];
}
if (currentsquare == 9) {
NSNumber* fnumb9 = [[NSNumber alloc] initWithInt:9];
[levelFinish addObject:fnumb9];
}
}
[startString release];
[finishString release];
//NOTE: I took out the intialization of control and level, both which work fine.
myGame = [[Game alloc] initLevel:level With:levelStart AndFinish:levelFinish WithRows:10 WithColumns:10 WithControl:control];
在我的 Game 类中,这是初始化方法:
-(id)initLevel:(int)aLevel With:(NSMutableArray*)starter AndFinish:(NSMutableArray*)finisher WithRows:(int)rows WithColumns:(int)cols WithControl:(Coord)aControlSquare{
self = [super init];
if (self != nil){
level = aLevel;
squares = [[NSMutableArray alloc] init];
squares = starter;
finish = [[NSMutableArray alloc] init];
finish = finisher;
}
return self;
}
现在它一直冻结的方法是在游戏中,由 UIgesture 识别器触发,这里是它冻结的行:
if ([[self squareForCoord:test] intValue] == 8) {
...
}
其中 squareForCord 获取测试坐标并返回该坐标处的 NSNumber。所以我确保我所有的测试都在界限内,并且它冻结的点在我的数组的范围内。我相信我正在正确初始化所有内容,但显然不是,这绝对不是发布发布的问题,所以我认为这一定是初始化问题,请帮忙。