Online Book Reader

Home Category

iPhone Game Development - Chris Craft [107]

By Root 1609 0

}

- (void) receiveData:(NSData *)remoteData fromPeer:(NSString *)peer

inSession: (GKSession *)session context:(void *)context {

int messageType;

[remoteData getBytes:&messageType length:sizeof(int)];

if (messageType == 0) {

StateData paddleData;

[remoteData getBytes:&paddleData length:sizeof(StateData)];

// Remote player should display on top so we need

// to invert the values

paddleData.paddleLocation.x = 320 - paddleData.paddleLocation.x;

paddleData.paddleLocation.y = 480 - paddleData.paddleLocation.y;

mainView.topPaddleData = paddleData;

// if we are a client we need to copy puck data sent from the host

if (mainView.connectionType == CLIENT_CONNECTION ) {

[mainView updatePuckData:paddleData];

}

[self sendData];

}

else {

EventData eventData;

[remoteData getBytes:&eventData length:sizeof(EventData)];

[mainView displayMessage:SCORE_MESSAGE forPlayer:TOP_PLAYER];

}

}

Note that receiveData inverts the paddle and puck positions that are received from the peer. This way, in peer-to-peer connections, each player controls the paddle on the bottom of the screen.

Back in the HockeyTableView class, the method didScore is called whenever a player scores a goal. We list this method here to highlight the fact that it calls out to the delegate method hockeyTableDidScore conditionally, depending on connection type:

- (void)didScore:(PlayerCode)playerCode {

if (messageView) return;

if ((bottomPaddleData.puckLocation.y > 480 + PUCK_DIAMETER)

| (bottomPaddleData.puckLocation.y < -PUCK_DIAMETER)) {

bottomPaddleData.puckVelocity.x = 0;

bottomPaddleData.puckVelocity.y = 0;

[self displayMessage:SCORE_MESSAGE forPlayer:playerCode];

if (connectionType == HOST_CONNECTION) {

[delegate hockeyTableDidScore:playerCode];

}

}

}

This method is needed because all puck movements are calculated on the host in peer-to-peer and the client never knows when the player scores. Calling out to this delegate sends the appropriate message to the client, as seen in the following method hockeyTableDidScore from HockeyViewController:

- (void)hockeyTableDidScore:(PlayerCode)playerCode {

EventData eventData;

eventData.packetType = 1;

NSData *dataPacket = [[NSData alloc] initWithBytes:&eventData

length:sizeof(EventData)];

[session sendDataToAllPeers:dataPacket withDataMode:GKSendDataReliable

error:nil];

}

Adding a splash of glitz

Earlier in the chapter we discussed how each client is notified when a goal is made. When a player does score, we added a circular “Goal” message that zooms in and out of view while rotating (Figure 7.19).

FIGURE 7.19

An animated message is displayed after a goal is scored.


A call to displayMessage starts this animation and plays it all the way through. If you run the app you will see that the message zooms out from the center and then pauses for a second. After the pause, the message expands and fades until it is no longer visible:

- (void)displayMessage:(MessageCode)messageCode forPlayer:(PlayerCode)playerCode {

UIImage *messageImage = goalImage;

if (messageCode == WIN_MESSAGE) {

if (messageCode == BOTTOM_PLAYER)

messageImage = connectionType =

CLIENT_CONNECTION ? redWinsImage : blueWinsImage;

else if (messageCode == TOP_PLAYER)

messageImage = connectionType =

CLIENT_CONNECTION ? blueWinsImage : redWinsImage;

}

messageView = [[UIImageView alloc] initWithImage:messageImage];

[goalImage release];

messageView.alpha = 0.0;

messageView.frame = CGRectMake(self.center.x, self.center.y, 0.0, 0.0);

[self addSubview:messageView];

[self bringSubviewToFront:messageView];

[UIView beginAnimations:nil context:messageView];

[UIView setAnimationDuration:0.75];

[UIView setAnimationCurve:UIViewAnimationCurveLinear];

[messageView setAlpha:0.5];

messageView.frame = CGRectMake(self.center.x-150.0, self.center.y-150.0,

300.0, 300.0);

messageView.tag = 0;

[UIView setAnimationDelegate:self];

[UIView setAnimationDidStopSelector:

@selector(animateMessageDidStop:finished:context:)

Return Main Page Previous Page Next Page

®Online Book Reader