我正在使用switch case语句来确定我在游戏中心多人游戏中收到的数据。
我看不出它有什么问题,但是当添加第二个案例陈述时,它就会停止工作。
我已声明:
typedef enum
{
kPacketTypeScore,
kPacketTypeReady,
} EPacketTypes;
typedef struct
{
EPacketTypes type;
size_t size;
} SPacketInfo;
typedef struct
{
SPacketInfo packetInfo;
int score;
} SScorePacket;
typedef struct
{
SPacketInfo packetInfo;
bool ready;
} SReadyPacket;
- (void)match:(GKMatch *)match didReceiveData:(NSData *)data fromPlayer:(NSString *)playerID
{
// first, assume it's the general SPacketInfo, that way we can access type and size
packet = (SPacketInfo*)[data bytes];
scoreData *scoreDat = [scoreData sharedData];
BOOL rdyReceived;
switch (packet->type)
{
case kPacketTypeScore:
{
SScorePacket* scorePacket = (SScorePacket*)packet;
[scoreLabel setString:[NSString stringWithFormat:@"You: %d Challenger: %d", scoreDat.score, scorePacket->score]];
break;
}
case kPacketTypeReady:
{
SReadyPacket* readyPacket = (SReadyPacket*)packet;
rdyReceived = readyPacket->ready;
if (rdyReceived == FALSE && scoreDat.mpRdy == TRUE) {
[rdyLabel setString:@"Waiting for challenger..."];
}
if (rdyReceived == TRUE && scoreDat.mpRdy == FALSE) {
[rdyLabel setString:@"Challenger is waiting... Ready?"];
}
if (rdyReceived == TRUE && scoreDat.mpRdy == TRUE) {
[[CCDirector sharedDirector] replaceScene:[CCTransitionFade transitionWithDuration:1.0 scene:[mpView node] withColor:ccWHITE]];
}
break;
}
default:
CCLOG(@"received unknown packet type %i (size: %u)", packet->type, packet->size);
break;
}
}
答案 0 :(得分:0)
您确定data
是SReadyPacket*
吗?
它可能是nil
或其他类型。
答案 1 :(得分:0)
您需要澄清数据的确切内容。您最初将其视为SPacketInfo
,以获取类型。然后将其转换为另外两个结构中的一个,包含 SPacketInfo
,但绝对不是SPacketInfo
。然后,当您尝试访问所述结构的成员时,您将访问完全未定义的位置。
packet
实际上是一个数据包结构,在这种情况下你应该将你的开关更改为:
switch (packet->packetInfo->type)
或者,它是SPacketInfo
,在这种情况下,您需要以其他方式获取实际的“数据包”。