ios - iPhone Multiplayer Code 有效,但 iPad 无效。委托(delegate)问题?

标签 ios objective-c cocos2d-iphone game-center multiplayer

我几乎完成了 iPhone 应用程序的多人游戏代码,并开始着手更新 iPad 版本的图形。令我惊讶的是,在更新我的所有 Sprite 后,我的 iPad 版本的行为与 iPhone 完全不同。我正在使用由自由开发人员和 Ray Wenderlich 的 GCHelper 编写的一些自定义代码的组合。到目前为止,一切都运行良好。对于 iphone 应用程序,我的 gamelayer 发送和接收所有玩家移动。在 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
}

主菜单.m

- (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 方法在游戏层而不是我的菜单场景中运行?

最佳答案

真是个傻子。 iPad iOS需要升级到6.0。

关于ios - iPhone Multiplayer Code 有效,但 iPad 无效。委托(delegate)问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14866789/

相关文章:

iphone - 在实例变量自动释放的控制台 (po) 中打印值

iOS 10 Today 扩展程序无法再读取共享用户默认值

ios - 使用导航 Controller 作为开始屏幕 ios

ios - Swift 4.2 委托(delegate)和协议(protocol)不起作用

iOS:枚举时对象相等

javascript - HTML5 Geolocation - 在 iOS 上一直不工作

ios - GKMatchmaker 自定义媒人 View Controller 已取消

iphone - 触摸开始 :(NSSet *)touches withEvent:(UIEvent *)event doesn't get called

iphone - cocos2D iPhone 中的 UItextfield 文本对齐问题

ios - Cocos2D 现实重力?