Game Development Reference
In-Depth Information
bogusScore++;
SScorePacket packet;
packet.type = kPacketTypeScore;
packet.score = bogusScore;
[[GameKitHelper sharedGameKitHelper] sendDataToAllPlayers:&packet
sizeInBytes:sizeof(packet)];
}
}
// send a tile coordinate
-(void) sendPosition:(CGPoint)tilePos
{
if ([GameKitHelper sharedGameKitHelper].currentMatch != nil)
{
SPositionPacket packet;
packet.type = kPacketTypePosition;
packet.position = tilePos;
[[GameKitHelper sharedGameKitHelper] sendDataToAllPlayers:&packet
sizeInBytes:sizeof(packet)];
}
}
The most important part of sending packets is to make sure you set the right packet
type. If you assign the wrong packet type, the receiver won't know what to do with the
packet. It might mistake it for a different type of packet, causing a crash because the re-
ceiver might try to access a nonexisting field. Or the receiver might simply work with
unrelated data, causing all kinds of bugs. Imagine the score becoming the player's posi-
tion, or vice versa. To avoid these kinds of issues, particularly if you have many differ-
ent packet types, creating methods like createPositionPacket and createS-
corePacket may be helpful, which you call with all the required parameters for the
packet while the method itself fills in the correct packet type.
In Figure 14-8 you can see the Tilemap16 project in action. Every time the player is
moved on the iPhone, a position packet is sent over the network. The iPad is connected
to the current match, receives the position packet, and moves the player character ac-
cordingly.
 
Search Nedrilad ::




Custom Search