我在通过 GKSession 发送整数数组时遇到问题。
我是这样做的:
发送它。
-(void)sendData {
NSMutableArray *myArray = [[NSMutableArray alloc] init];
[myArray addObject:[NSNumber numberWithInteger:snakeHead.position.x]];
[myArray addObject:[NSNumber numberWithInteger:snakeHead.position.y]];
[myArray addObject:[NSNumber numberWithInteger:direction.x]];
[myArray addObject:[NSNumber numberWithInteger:direction.y]];
[myArray addObject:[NSNumber numberWithInteger:bodyOffsetX]];
[myArray addObject:[NSNumber numberWithInteger:bodyOffsetY]];
[myArray addObject:[NSNumber numberWithInteger:amountBodies]];
NSData* encodedArray = [NSKeyedArchiver archivedDataWithRootObject:myArray];
[snakeSession sendData:encodedArray toPeers:snakePeers withDataMode:GKSendDataReliable
error:nil];
[encodedArray release];
}
从调度程序调用 sendData 函数。
去接收它。
-(void)receiveData:(NSData *)data fromPeer:(NSString *)peer inSession:(GKSession *)session
context:(void *)context {
NSArray *hisArray = [NSKeyedUnarchiver unarchiveObjectWithData:data];
snakeHead2.position = ccp([[hisArray objectAtIndex:0] integerValue],
[[hisArray objectAtIndex:1] integerValue]);
direction2 = ccp([[hisArray objectAtIndex:2] integerValue],
[[hisArray objectAtIndex:3] integerValue]);
bodyOffsetX2 = [[hisArray objectAtIndex:4] integerValue];
bodyOffsetY2 = [[hisArray objectAtIndex:5] integerValue];
amountBodies2 = [[hisArray objectAtIndex:6] integerValue];
}
这不起作用,游戏崩溃。
那么我在这里做错了什么?