Switch Case 语句问题

发布于 2024-10-12 07:33:42 字数 1917 浏览 6 评论 0原文

我正在使用 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;
    }
}

I am using a switch case statement to determine what data I am receiving in game center multiplayer.

I can't see whats wrong with it but when adding the second case statement it stops working.

I have declared this:

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;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

深巷少女 2024-10-19 07:33:42

您确定 dataSReadyPacket* 吗?
它可能是 nil 或其他类型。

Are you sure that data is a SReadyPacket*?
It may be nil or some other type.

往日情怀 2024-10-19 07:33:42

您需要澄清传入的数据到底是什么。您最初将其视为 SPacketInfo,以获取类型。然后将其转换为其他两个结构之一,其中包含一个SPacketInfo,但绝对不是SPacketInfo本身。然后,当您尝试访问所述结构的成员时,您将访问完全未定义的位置。

packet 实际上是一个数据包结构,在这种情况下,您应该将开关更改为:

switch (packet->packetInfo->type)

或者,它是一个 SPacketInfo,在这种情况下,您需要获取实际的以其他方式“包”。

You need to clarify what exactly the data coming in is. You initially treat it as a SPacketInfo, to get the type. Then cast it to one of two other structs, which contain an SPacketInfo, but are definitely not SPacketInfo's themselves. Then when you try to access members of said struct, you're accessing completely undefined locations.

Either packet is actually a packet struct, in which case you should change your switch to:

switch (packet->packetInfo->type)

Or, it is a SPacketInfo, in which case you'll need to get the actual "packet" in some other way.

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