Game Development Reference
In-Depth Information
SBasePacket to allow the receiver to inspect the packet type , and based on that,
the receiver can then safely cast the struct to the actual packet.
Listing 14-26 shows an example of this. It's the onReceivedData method from the
TileMapLayer class.
Listing 14-26 . Receiving Packets and Determining Packet Type
-(void) onReceivedData:(NSData*)data fromPlayer:(NSString*)playerID
{
SBasePacket* basePacket = (SBasePacket*)data.bytes;
switch (basePacket-> type)
{
case kPacketTypeScore:
{
SScorePacket* scorePacket = (SScorePacket*)basePacket;
CCLOG(@"\tscore = %i", scorePacket-> score);
break;
}
case kPacketTypePosition:
{
SPositionPacket* positionPacket = (SPositionPacket*)basePacket;
if (playerID ! = [GKLocalPlayer localPlayer].playerID)
{
CCTMXTiledMap* tileMap =
(CCTMXTiledMap*)[self getChildByTag:TileMapNode];
[self centerTileMapOnTileCoord:positionPacket-> position
tileMap:tileMap];
}
break;
}
default:
CCLOG(@"unknown packet type %i", basePacket-> type);
break;
 
 
Search Nedrilad ::




Custom Search