Int 显示为长奇值

发布于 2024-10-12 11:43:47 字数 1327 浏览 6 评论 0原文

我正在尝试在我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

回首观望 2024-10-19 11:43:47

尝试:

[scoreLabel setString:[NSString stringWithFormat:@"You: %d Challenger: %d", score.score, scorePacket->score]]; // Note: scorePacket.score

您正在尝试打印指向分数包的指针,而不是数据包中的分数。

Try:

[scoreLabel setString:[NSString stringWithFormat:@"You: %d Challenger: %d", score.score, scorePacket->score]]; // Note: scorePacket.score

You're were trying to print a pointer-to-the-scorePacket, not the score in-the-packet.

鹤仙姿 2024-10-19 11:43:47

需要scorePacket->score而不是scorePacket。打印错误的东西。

Needed scorePacket->score rather than scorePacket. Printing the wrong thing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文