我正试图在我的iphone游戏中为游戏中心多人游戏发送一个int。
整数出现并显示为奇数长整数值而不是预期值。
我的.h:
typedef enum
{
kPacketTypeScore,
} EPacketTypes;
typedef struct
{
EPacketTypes type;
size_t size;
} SPacketInfo;
typedef struct
{
SPacketInfo packetInfo;
int score;
} SScorePacket;
然后.m:
发送数据:
scoreData *score = [scoreData sharedData];
SScorePacket packet;
packet.packetInfo.type = kPacketTypeScore;
packet.packetInfo.size = sizeof(SScorePacket);
packet.score = score.score;
NSData* dataToSend = [NSData dataWithBytes:&packet length:packet.packetInfo.size];
NSError *error;
[self.myMatch sendDataToAllPlayers: dataToSend withDataMode: GKMatchSendDataUnreliable error:&error];
if (error != nil)
{
// handle the error
}
接收:
SPacketInfo* packet = (SPacketInfo*)[data bytes];
switch (packet->type)
{
case kPacketTypeScore:
{
SScorePacket* scorePacket = (SScorePacket*)packet;
scoreData *score = [scoreData sharedData];
[scoreLabel setString:[NSString stringWithFormat:@"You: %d Challenger: %d", score.score, scorePacket]];
break;
}
default:
CCLOG(@"received unknown packet type %i (size: %u)", packet->type, packet->size);
break;
}
有什么想法吗?感谢。
答案 0 :(得分:1)
尝试:
[scoreLabel setString:[NSString stringWithFormat:@"You: %d Challenger: %d", score.score, scorePacket->score]]; // Note: scorePacket.score
你正试图打印一个指向分数的指针,而不是分组中的分数。
答案 1 :(得分:0)
得分scorePacket->得分而不是scorePacket。打印错误的东西。