Game Development Reference
In-Depth Information
[currentMatch sendDataToAllPlayers:packet
withDataMode:GKMatchSendDataUnreliable
error:&error];
[self setLastError:error];
}
-(void) match:(GKMatch*)match didReceiveData:(NSData*)data ←
fromPlayer:(NSString*)playerID
{
if ([delegate respondsToSelector:@selector(onReceivedData:fromPlayer:)])
{
[delegate onReceivedData:data fromPlayer:playerID];
}
}
The sendDataToAllPlayers method takes a void pointer as input and wraps it
into an NSData object. You can send any data as long as you provide the correct
length of that data packet. Typically, networked programs send structs like CGPoint
(or any custom struct) to make this process easier, because you can then use
sizeof(myPoint) to get the length (size in bytes) of such a data structure.
Also, to speed up transmission, most data is sent unreliably. Data that's sent frequently
can especially be sent unreliably because if a packet ever gets lost, the clients simply
have to wait for the next packet to arrive. If you do need every packet to arrive—for
example, because it contains crucial information that's sent only once—then you
should set the data mode to GKMatchSendDataReliable . This instructs GameKit
to simply transmit the packet again if it couldn't be delivered. Because GameKit has to
receive a return packet from clients to acknowledge that they received the packet, this
adds additional traffic.
What data you should send and how often you should send it depend entirely on the
game itself. The ground rule is to send as little as you can, as rarely as possible. For ex-
ample, instead of transmitting each player's position every frame, you should send a
packet for each movement action, because the movement in the tilemap game is always
32 pixels in one direction and done by a CCMoveAction . So, it's sufficient to send
when the move should start and in which direction it should be—that saves a lot of
traffic compared to sending each player's position every frame.
Search Nedrilad ::




Custom Search