我已经完成了我的 iPhone 应用程序的多人游戏代码,并开始着手更新 iPad 版本的图形。更新完所有精灵后,令我惊讶的是,我的 iPad 版本的行为与 iPhone 完全不同。我正在使用由自由开发人员和 Ray Wenderlich 的 GCHelper 编写的一些自定义代码的组合。到目前为止,一切都运行良好。对于 iphone 应用程序,我的游戏层发送和接收所有玩家动作。在 iPad 应用程序中,MainMenu场景出于某种原因正在接收数据。
主菜单.h
@protocol GCHelperDelegate
- (void)matchStarted;
- (void)matchEnded;
- (void)match:(GKMatch *)match didReceiveData:(NSData *)data fromPlayer:(NSString *)playerID;
- (void)inviteReceived;
@end
@interface MainMenu: CCLayer <GCHelperDelegate>
{//some code
}
主菜单
- (void) StartMultiplayerGame //button calls up gamecenter matchmaking
{
NSLog(@"***In StartMultiplayerGame***");
delegate1 = (AppDelegate *) [UIApplication sharedApplication].delegate;
[[GCHelper sharedInstance] findMatchWithMinPlayers:2 maxPlayers:4 viewController:delegate1.viewController delegate:self];
boo_multiplayer=true;
boo_startgame=true;
colorLayer = [CCLayerColor layerWithColor:ccc4(0, 0, 0, 255)];
[colorLayer setOpacity:175];
[self addChild:colorLayer z:2];
return;
}
GCHelper.h
#import <Foundation/Foundation.h>
#import <GameKit/GameKit.h>
#import "cocos2d.h"
#import "AppDelegate.h"
@protocol GCHelperDelegate
- (void)matchStarted;
- (void)matchEnded;
- (void)match:(GKMatch *)match didReceiveData:(NSData *)data fromPlayer:(NSString *)playerID;
- (void)inviteReceived;
@end
@interface GCHelper : NSObject <GKMatchmakerViewControllerDelegate, GKMatchDelegate> {
BOOL gameCenterAvailable;
BOOL userAuthenticated;
bool boo_invite;
UIViewController *presentingViewController;
GKMatch *match;
BOOL matchStarted;
__strong id <GCHelperDelegate> delegate;
NSMutableDictionary *playersDict;
GKInvite *pendingInvite;
NSArray *pendingPlayersToInvite;
NSMutableArray *playernames;
NSMutableArray *playeridentification;
NSError *lastError;
GKMatchRequest *request;
// // AppDelegate *delegate1;
}
@property (assign, readonly) BOOL gameCenterAvailable;
@property (strong) UIViewController *presentingViewController;
@property (strong) GKMatch *match;
@property (strong) id <GCHelperDelegate> delegate;
@property (strong) NSMutableDictionary *playersDict;
@property (strong) GKInvite *pendingInvite;
@property (strong) NSArray *pendingPlayersToInvite;
+ (GCHelper *)sharedInstance;
- (void)authenticateLocalUser;
- (void)findMatchWithMinPlayers:(int)minPlayers maxPlayers:(int)maxPlayers viewController:(UIViewController *)viewController delegate:(id<GCHelperDelegate>)theDelegate;
@end
GCHelper.m
- (void)findMatchWithMinPlayers:(int)minPlayers maxPlayers:(int)maxPlayers viewController:(UIViewController *)viewController delegate:(id<GCHelperDelegate>)theDelegate
{
if (!gameCenterAvailable) return;
matchStarted = NO;
self.match = nil;
self.presentingViewController = viewController;
delegate = theDelegate;
if (pendingInvite != nil) {
[presentingViewController dismissModalViewControllerAnimated:NO];
GKMatchmakerViewController *mmvc = [[GKMatchmakerViewController alloc] initWithInvite:pendingInvite];
mmvc.matchmakerDelegate = self;
[presentingViewController presentModalViewController:mmvc animated:YES];
self.pendingInvite = nil;
self.pendingPlayersToInvite = nil;
}
else {
[presentingViewController dismissModalViewControllerAnimated:NO];
request=[[GKMatchRequest alloc] init];
request.minPlayers = minPlayers;
request.maxPlayers = maxPlayers;
request.playersToInvite = pendingPlayersToInvite;
GKMatchmakerViewController *mmvc = [[GKMatchmakerViewController alloc] initWithMatchRequest:request];
mmvc.matchmakerDelegate = self;
[presentingViewController presentModalViewController:mmvc animated:YES];
self.pendingInvite = nil;
self.pendingPlayersToInvite = nil;
}
}
// A peer-to-peer match has been found, the game should start
- (void)matchmakerViewController:(GKMatchmakerViewController *)viewController didFindMatch:(GKMatch *)theMatch {
[presentingViewController dismissModalViewControllerAnimated:YES];
self.match = theMatch;
self.match.delegate = self;
currentMatch=match;
if (!matchStarted && match.expectedPlayerCount == 0)
{
NSLog(@"Ready to start match!");
TXGameCenterManager * gameCenterManager = [TXGameCenterManager sharedTXGameCenterManager];
gameCenterManager.multiplayerMatch = match;
// The delegate of the match is HelloWorldLayer
gameCenterManager.multiplayerMatch.delegate = self;
AppDelegate * delegate1 = (AppDelegate *) [UIApplication sharedApplication].delegate;
[delegate1.viewController dismissModalViewControllerAnimated:NO];
[self lookupPlayers];
}
}
- (void)lookupPlayers
{
NSLog(@"Looking up %d players...", match.playerIDs.count);
[GKPlayer loadPlayersForIdentifiers:match.playerIDs withCompletionHandler:^(NSArray *players, NSError *error) {
if (error != nil) {
NSLog(@"Error retrieving player info: %@", error.localizedDescription);
matchStarted = NO;
//[delegate matchEnded];
}
else
{
// some code.
[[CCDirector sharedDirector] replaceScene:[HelloWorldLayer node]];
}
我真的不明白 iPad 模拟器工作得很好。我的应用程序在这里发生了什么,如何强制 didReceiveData 方法在游戏层而不是菜单场景中运行?