【问题标题】:Objective-C unable to call a function in Game CenterObjective-C 无法在 Game Center 中调用函数
【发布时间】:2013-01-01 23:56:06
【问题描述】:

我在尝试通过 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。

【问题讨论】:

    标签: objective-c ios call game-center


    【解决方案1】:

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

    【讨论】:

    • 绝对正确-我没有在racetohundredViewController.m文件的viewDidLoad部分实例化它-非常感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 2020-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-06
    • 2020-06-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多