在两个设备之间发送时数据会被弄乱
Data gets messed up when sent between two devices
我构建了一个多人游戏,它在我的 iPhone 5s 上与模拟器玩得很好。但是每当我尝试从 5s 对抗 iPhone 5 时,都会出现一些问题。
游戏向其他玩家发送两个值:得分和等级。
如果 iPhone 5s 发送值:iPhone 5 认为 score = 0 并且 level = 我们作为分数发送的值。
typedef struct {
NSUInteger score;
NSUInteger level;
} MessageNewScore;
- (void)sendData:(NSData *)data {
NSError *error;
GameKitHelper *gameKitHelper = [GameKitHelper sharedGameKitHelper];
BOOL success = [gameKitHelper.match sendDataToAllPlayers:data withDataMode:GKMatchSendDataReliable error:&error];
if (!success) {
NSLog(@"Error sending data:%@", error.localizedDescription);
}
}
// Run on iPhone 5s
- (void)sendNewScore:(NSUInteger)score level:(NSUInteger)level{
NSLog(@"score: %lu", (unsigned long)score); // E.g. 1930
NSLog(@"level: %lu", (unsigned long)level); // E.g. 7
MessageNewScore messageNewScore;
messageNewScore.message.messageType = kMessageTypeNewScore;
messageNewScore.score = score;
messageNewScore.level = level;
NSData *data = [NSData dataWithBytes:&messageNewScore
length:sizeof(MessageNewScore)];
[self sendData:data];
NSLog(@"score: %lu", (unsigned long)messageNewScore.score); // 1930
NSLog(@"level: %lu", (unsigned long)messageNewScore.level); // 7
}
// Run on iPhone 5
- (void)match:(GKMatch *)match didReceiveData:(NSData *)data fromPlayer:(NSString *)playerID {
MessageNewScore *messageNewScore = (MessageNewScore*)[data bytes];
NSLog(@"score: %lu", (unsigned long)messageNewScore->score); // According to iPhone 5 this is 0
NSLog(@"level: %lu", (unsigned long)messageNewScore->level); // According to iPhone 5 this is 1930
[_delegate enemysScoreReceived:messageNewScore->score level:messageNewScore->level];
}
在最后一种情况下,sendNewScore::
中登录的值是正确的,而 didReceiveData::
中的值是错误的。
我认为这可能与 5s 中的 64 位处理器有关,但不确定如何解决此问题。
iPhone 5 使用 Apple A6(32 位)处理器,iPhone 5S 使用 Apple A7(64 位)处理器。
你需要 encode/decode NSData
在玩家之间传送的便携表示,比如可以通过 NSKeyedArchiver
/NSKeyedUnarchiver
获得 encoding/decoding NSInteger
s 等。您当前正在发送 struct
s,其表示是特定于处理器的。
我构建了一个多人游戏,它在我的 iPhone 5s 上与模拟器玩得很好。但是每当我尝试从 5s 对抗 iPhone 5 时,都会出现一些问题。
游戏向其他玩家发送两个值:得分和等级。
如果 iPhone 5s 发送值:iPhone 5 认为 score = 0 并且 level = 我们作为分数发送的值。
typedef struct {
NSUInteger score;
NSUInteger level;
} MessageNewScore;
- (void)sendData:(NSData *)data {
NSError *error;
GameKitHelper *gameKitHelper = [GameKitHelper sharedGameKitHelper];
BOOL success = [gameKitHelper.match sendDataToAllPlayers:data withDataMode:GKMatchSendDataReliable error:&error];
if (!success) {
NSLog(@"Error sending data:%@", error.localizedDescription);
}
}
// Run on iPhone 5s
- (void)sendNewScore:(NSUInteger)score level:(NSUInteger)level{
NSLog(@"score: %lu", (unsigned long)score); // E.g. 1930
NSLog(@"level: %lu", (unsigned long)level); // E.g. 7
MessageNewScore messageNewScore;
messageNewScore.message.messageType = kMessageTypeNewScore;
messageNewScore.score = score;
messageNewScore.level = level;
NSData *data = [NSData dataWithBytes:&messageNewScore
length:sizeof(MessageNewScore)];
[self sendData:data];
NSLog(@"score: %lu", (unsigned long)messageNewScore.score); // 1930
NSLog(@"level: %lu", (unsigned long)messageNewScore.level); // 7
}
// Run on iPhone 5
- (void)match:(GKMatch *)match didReceiveData:(NSData *)data fromPlayer:(NSString *)playerID {
MessageNewScore *messageNewScore = (MessageNewScore*)[data bytes];
NSLog(@"score: %lu", (unsigned long)messageNewScore->score); // According to iPhone 5 this is 0
NSLog(@"level: %lu", (unsigned long)messageNewScore->level); // According to iPhone 5 this is 1930
[_delegate enemysScoreReceived:messageNewScore->score level:messageNewScore->level];
}
在最后一种情况下,sendNewScore::
中登录的值是正确的,而 didReceiveData::
中的值是错误的。
我认为这可能与 5s 中的 64 位处理器有关,但不确定如何解决此问题。
iPhone 5 使用 Apple A6(32 位)处理器,iPhone 5S 使用 Apple A7(64 位)处理器。
你需要 encode/decode NSData
在玩家之间传送的便携表示,比如可以通过 NSKeyedArchiver
/NSKeyedUnarchiver
获得 encoding/decoding NSInteger
s 等。您当前正在发送 struct
s,其表示是特定于处理器的。