Game Development Reference
In-Depth Information
How to do it
Following are the steps involved for implementing the game play and deciding who wins or
loses:
1. We will be detecting the collision in the update method of the GameScene by us-
ing a method called CGRectIntersectsRect . In this, we can pass the two
frames of nodes and it will check if the two frames intersect with each other or not.
With this check, we will only check this for local player collision, and if collision
happens update the game state to kGameStateComplete and show the local
player You Won who has reached the finish line. Also, as the game has ended, to
auto start the game, call a method called restartGameAfterSomeTime ,
which we will understand as we proceed.
2. After this, the local player is shown the correct result and the game is restarted, but
as it is a multiplayer game, the reaction of this collision should also be reflected on
the other device. So, pass a packet with a NetworkPacketCode named KNet-
workPacketCodePlayerLost which will be sent to the other player who has
lost the game. Following is the codes to achieve this:
#pragma mark - Update Loop Method
-(void)update:(CFTimeInterval)currentTime {
/* Called before each frame is rendered */
CGRect blueFinishLineFrame =
CGRectMake(0, self.frame.size.height * 0.15,
self.frame.size.width, 1);
CGRect redFinishLineFrame = CGRectMake(0,
self.frame.size.height * 0.85, self.frame.size.width,
1);
if (self.localTankSprite == self.blueTankSprite &&
CGRectIntersectsRect(self.localTankSprite.frame,
blueFinishLineFrame))
{
self.gameState = kGameStateComplete;
Search WWH ::




Custom Search