Online Book Reader

Home Category

iPhone Game Development - Chris Craft [108]

By Root 1629 0
];

[UIView commitAnimations];

}

By having one animation begin after another ends, we create a nice effect for the game. This is accomplished by assigning animateMessageDidStop as the selector in the call to the setAnimationDidStopSelector method above. Notice that animateMessageDidStop also points its selector back to itself, which provides a nice place to do some cleanup:

- (void)animateMessageDidStop:(NSString *)animationID finished:

(NSNumber *)finished context:(void *)context {

if (messageView.tag == 1) {

bottomPaddleData.puckLocation = CGPointMake(160, 250);

bottomPaddleData.puckVelocity = CGPointMake(0, 0);

[messageView release];

messageView = nil;

return;

}

[UIView beginAnimations:nil context:messageView];

[UIView setAnimationDelay:2.0];

[UIView setAnimationDuration:0.75];

[UIView setAnimationCurve:UIViewAnimationCurveLinear];

[messageView setAlpha:0.0];

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

1500.0, 1500.0);

messageView.tag = 1;

[UIView setAnimationDelegate:self];

[UIView setAnimationDidStopSelector:

@selector(animateMessageDidStop:finished:context:) ];

[UIView commitAnimations];

}

During the entire animation, the message is rotated. The rotation is applied to the CALayer of the UIImageView in the drawMethod you saw earlier:

if (messageView) {

messageView.layer.transform = CATransform3DMakeRotation(messageRotation,

0., 0., 1.);

}

Reviewing the last pieces

A handful of helper methods are used to abstract out some of the common logic needed throughout the previous code listings. They are listed here to help connect the last dots of the code in HockeyTableView. Listing 7.6 returns a normal vector between x1, y1, and x2; y2 is used to represent direction. It then returns the distance between two points, calculates the speed of the paddle, and prevents the paddle from sliding past its boundary.

Listing 7.6

Helper Methods Used in AmuckPuck

CGPoint normalVector(double x1, double x2, double y1, double y2) {

double run = x1 - x2;

double rise = y1 - y2;

double total = abs(rise) + abs(run);

CGPoint vector;

vector.x = run / total;

vector.y = rise / total;

return vector;

}

double distance(double x1, double x2, double y1, double y2) {

return sqrt(pow(x1 -x2, 2) + pow(y1 - y2, 2));

}

double paddleSpeed(StateData stateData) {

double speed = sqrt(pow(stateData.paddleVelocity.x, 2)

+ pow(stateData.paddleVelocity.y, 2));

speed = abs(speed * PADDLE_STRIKE);

if (speed > MAX_PADDLE_STRIKE)

return MAX_PADDLE_STRIKE;

if (speed < MIN_PADDLE_STRIKE)

return MIN_PADDLE_STRIKE;

return speed;

}

void correctTouchPoint(CGPoint *touchPoint) {

if ((*touchPoint).x - PADDLE_RADIUS < 0)

(*touchPoint).x = PADDLE_RADIUS;

if ((*touchPoint).x + PADDLE_RADIUS > 320)

(*touchPoint).x = 320 - PADDLE_RADIUS;

if ((*touchPoint).y - PADDLE_RADIUS < 0)

(*touchPoint).y = PADDLE_RADIUS;

if ((*touchPoint).y + PADDLE_RADIUS > 480)

(*touchPoint).y = 480 - PADDLE_RADIUS;

}

@end

Connecting to players with peer-to-peer

The HockeyViewController is the final class we will be discussing. This class is responsible for managing our GKSession and GKPeerPickerController objects. The process is the same as it was in P2P Chat, so you should find the code familiar.

Sending and receiving messages

The methods connectionType and startNearby are straightforward but noteworthy. When a player selects a game type from the menu, these methods are called to record connectionType internally. Peer-to-peer connections start as a host connection and then one peer switches to the client during the connection process:

- (void)startNearby {

connectionType = HOST_CONNECTION;

}

- (void)startHeadToHead {

connectionType = HEADTOHEAD_CONNECTION;

}

The receiveData method is called by your GKSession whenever data is received from a peer. This method was listed in the preceding code to demonstrate how paddle data is sent to the client in a peer-to-peer connection. When

Return Main Page Previous Page Next Page

®Online Book Reader