我有點噩夢試圖通過Game Center實現讓兩個玩家在我的iPhone應用程序上相互對戰的能力。Objective-C無法在Game Center中調用函數
我只是試圖從我有一個不同的.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 reliable: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;
最後.m文件的削減版本GameCenterManager:
#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。
絕對正確 - 我沒有在racetohundredViewController.m文件的viewDidLoad部分實例化它 - 非常感謝您的幫助! – user1309044