0

我正在做一场噩梦,试图通过 Game Center 实现让两个玩家在我的 iPhone 应用程序上互相对战的能力。

我只是想从我拥有的不同 .m 文件中访问 GameCenterManager.m 中的函数。在我的 racetohundredViewController.h 文件中,我有:

#import <UIKit/UIKit.h>
#import "startPage.h"
#import "GameCenterManager.h"
#import <GameKit/GameKit.h>

@interface racetohundredViewController : UIViewController <UIActionSheetDelegate,     GKLeaderboardViewControllerDelegate, GameCenterManagerDelegate>

{
    GameCenterManager *gcManager;
    BOOL gameIsMultiplayer;
    double randomHostNumber;
}
@property (retain, nonatomic) GameCenterManager *gcManager;

在我相应的 .m 文件中,我有:

@interface racetohundredViewController ()
@end

@implementation racetohundredViewController
@synthesize gcManager;

- (void)generateAndSendHostNumber;
{
    NSLog(@"Generate and send host number");
    randomHostNumber = arc4random();
    NSString *randomNumberString = [NSString stringWithFormat: @"$Host:%f", randomHostNumber];
    NSLog(@"the random number string is: %@", randomNumberString);
    [self.gcManager testString];
    [self.gcManager sendStringToAllPeers:randomNumberString reliable: YES];
}

- (void)viewDidLoad
{
[self generateAndSendHostNumber];
}

这是 [self.gcManager testString]; 和 [self.gcManager sendStringToAllPeers:randomNumberString 可靠:YES];没有被调用。他们以前,我显然以某种方式搞砸了。我可以看到随机数字符串的 NSLog。

在我的 GameCenterManager.h 文件中(包括一些与此问题无关的位):

#import <Foundation/Foundation.h>
#import <GameKit/GameKit.h>

@class GKLeaderboard, GKAchievement, GKPlayer;
@protocol GameCenterManagerDelegate <NSObject>
@optional
- (void) processGameCenterAuthentication: (NSError*) error;
- (void) scoreReported: (NSError*) error;
- (void) reloadScoresComplete: (GKLeaderboard*) leaderBoard error: (NSError*) error;
- (void) achievementSubmitted: (GKAchievement*) ach error:(NSError*) error;
- (void) achievementResetResult: (NSError*) error;
- (void) mappedPlayerIDToPlayer: (GKPlayer*) player error: (NSError*) error;
- (void) receivedData:(NSDictionary *)dataDictionary;
@end

@interface GameCenterManager : NSObject <GameCenterManagerDelegate>
{
    id <GameCenterManagerDelegate, NSObject> delegate;
    NSMutableDictionary* earnedAchievementCache;
    id matchOrSession;
}

//This property must be atomic to ensure that the cache is always in a viable state...
@property (retain) NSMutableDictionary* earnedAchievementCache;
@property (nonatomic, strong) id <GameCenterManagerDelegate, NSObject> delegate;
@property(nonatomic, strong) id matchOrSession;

+ (BOOL) isGameCenterAvailable;
- (void) authenticateLocalUser;
- (void)sendStringToAllPeers:(NSString *)dataString reliable:(BOOL)reliable;
- (void)sendString:(NSString *)dataString toPeers:(id)peers reliable: (BOOL)reliable;
-(void)testString;

最后是 GameCenterManager 的 .m 文件的精简版:

#import "GameCenterManager.h"
#import <GameKit/GameKit.h>
@implementation GameCenterManager
@synthesize earnedAchievementCache;
@synthesize delegate;
@synthesize matchOrSession;


-(void)testString
{
    NSLog(@"THIS IS A TEST");
}

我也可以发布有关 sendString 的代码,但原则是即使使用基本的 testString 函数,它也不会调用它。

我在这里遗漏了一些非常明显的东西吗?似乎当我单步执行代码时,它会突出显示 GameCenterManager 的 .h 文件中的函数,但它不会按照我的要求显示 NSLog。

4

1 回答 1

2

我最好的猜测self.gcManagernil。你在哪里实例化你的GameCenterManager

于 2013-01-02T00:34:11.040 回答