我正在使用 switch case 语句来确定我在游戏中心多人游戏中收到的数据。
我看不出它有什么问题,但是当添加第二个 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;
}
}