Int 显示为长奇值
我正在尝试在我的 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;
}
有什么想法吗?谢谢。
I am trying to send an int in my iphone game for game center multiplayer.
The integer is coming up and appearing as an odd long integer value rather than the expected one.
I have this in my .h:
typedef enum
{
kPacketTypeScore,
} EPacketTypes;
typedef struct
{
EPacketTypes type;
size_t size;
} SPacketInfo;
typedef struct
{
SPacketInfo packetInfo;
int score;
} SScorePacket;
Then .m:
Sending data:
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
}
Receiving:
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;
}
Any ideas? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试:
您正在尝试打印指向分数包的指针,而不是数据包中的分数。
Try:
You're were trying to print a pointer-to-the-scorePacket, not the score in-the-packet.
需要scorePacket->score而不是scorePacket。打印错误的东西。
Needed scorePacket->score rather than scorePacket. Printing the wrong thing.