在cocos2d游戏中集成Gamecenter

时间:2011-06-08 11:34:46

标签: cocos2d-iphone game-center

有没有人知道如何在Cocos2d中集成游戏中心。 请告诉我步骤,以便我可以将其整合到我的游戏中。

3 个答案:

答案 0 :(得分:30)

<强>更新

我创建了自己的帮助程序类,适用于所有类型的应用程序(也是Cocos2D 1&amp; 2+) https://github.com/alexblunck/ABGameKitHelper


您好我建议您使用Steffen Itterheim的GKHelper Class!我为您上传了GKHelper.h / GKHelper.m:http://www.cl.ly/7ReW

然后按照以下说明操作:

// 0.0将GameKit框架添加到项目中(询问您是否知道如何执行此操作;))

// 0。更改“[window addSubview:viewController.view];”在AppDelegate.m中: //如果您在0.99.5之后使用任何cocos2D版本,请执行此操作:

window.rootViewController = viewController;

// 1。将Gamekithelper.h / .m添加到项目

// 2。在给定标题中包含以下委托:

<GameKitHelperProtocol>

// 3。将委托方法添加到.m

// 4。将GameKitHelper添加到“场景”:

GameKitHelper *gkHelper = [GameKitHelper sharedGameKitHelper];
gkHelper.delegate = self;
[gkHelper authenticateLocalPlayer];

//将分数添加到排行榜:

GameKitHelper *gkHelper = [GameKitHelper sharedGameKitHelper];
[gkHelper submitScore:scoreValue category:@"LeaderboardID"];

//添加成就完成:

GameKitHelper *gkHelper = [GameKitHelper sharedGameKitHelper];
[gkHelper reportAchievementWithID:@"AchievementID" percentComplete:100];

这些是在步骤3中提到的需要添加的委托方法:

#pragma mark GameKitHelper delegate methods
-(void) onLocalPlayerAuthenticationChanged
{
    GKLocalPlayer* localPlayer = [GKLocalPlayer localPlayer];
    CCLOG(@"LocalPlayer isAuthenticated changed to: %@", localPlayer.authenticated ? @"YES" : @"NO");

    if (localPlayer.authenticated)
    {
        GameKitHelper* gkHelper = [GameKitHelper sharedGameKitHelper];
        [gkHelper getLocalPlayerFriends];
        //[gkHelper resetAchievements];
    }   
}
-(void) onFriendListReceived:(NSArray*)friends
{
    CCLOG(@"onFriendListReceived: %@", [friends description]);
    GameKitHelper* gkHelper = [GameKitHelper sharedGameKitHelper];
    [gkHelper getPlayerInfo:friends];
}
-(void) onPlayerInfoReceived:(NSArray*)players
{
    CCLOG(@"onPlayerInfoReceived: %@", [players description]);


}
-(void) onScoresSubmitted:(bool)success
{
    CCLOG(@"onScoresSubmitted: %@", success ? @"YES" : @"NO");
}
-(void) onScoresReceived:(NSArray*)scores
{
    CCLOG(@"onScoresReceived: %@", [scores description]);
    GameKitHelper* gkHelper = [GameKitHelper sharedGameKitHelper];
    [gkHelper showAchievements];
}
-(void) onAchievementReported:(GKAchievement*)achievement
{
    CCLOG(@"onAchievementReported: %@", achievement);
}
-(void) onAchievementsLoaded:(NSDictionary*)achievements
{
    CCLOG(@"onLocalPlayerAchievementsLoaded: %@", [achievements description]);
}
-(void) onResetAchievements:(bool)success
{
    CCLOG(@"onResetAchievements: %@", success ? @"YES" : @"NO");
}
-(void) onLeaderboardViewDismissed
{
    CCLOG(@"onLeaderboardViewDismissed");

    GameKitHelper* gkHelper = [GameKitHelper sharedGameKitHelper];
    [gkHelper retrieveTopTenAllTimeGlobalScores];
}
-(void) onAchievementsViewDismissed
{
    CCLOG(@"onAchievementsViewDismissed");
}
-(void) onReceivedMatchmakingActivity:(NSInteger)activity
{
    CCLOG(@"receivedMatchmakingActivity: %i", activity);
}
-(void) onMatchFound:(GKMatch*)match
{
    CCLOG(@"onMatchFound: %@", match);
}
-(void) onPlayersAddedToMatch:(bool)success
{
    CCLOG(@"onPlayersAddedToMatch: %@", success ? @"YES" : @"NO");
}
-(void) onMatchmakingViewDismissed
{
    CCLOG(@"onMatchmakingViewDismissed");
}
-(void) onMatchmakingViewError
{
    CCLOG(@"onMatchmakingViewError");
}
-(void) onPlayerConnected:(NSString*)playerID
{
    CCLOG(@"onPlayerConnected: %@", playerID);
}
-(void) onPlayerDisconnected:(NSString*)playerID
{
    CCLOG(@"onPlayerDisconnected: %@", playerID);
}
-(void) onStartMatch
{
    CCLOG(@"onStartMatch");
}
-(void) onReceivedData:(NSData*)data fromPlayer:(NSString*)playerID
{
    CCLOG(@"onReceivedData: %@ fromPlayer: %@", data, playerID);
}

答案 1 :(得分:3)

您可以使用GamKit框架。游戏中心也非常强大,可以管理您的在线游戏和游戏分数。有了游戏中心,你可以创建两种类型的游戏

1:实时比赛(实时赛车) 2:转动基础比赛(在线纸牌游戏)

我与你分享RaywenderLich的链接:

实时匹配:http://www.raywenderlich.com/3276/game-center-tutorial-for-ios-how-to-make-a-simple-multiplayer-game-part-12

基于回合的比赛http://www.raywenderlich.com/5480/beginning-turn-based-gaming-with-ios-5-part-1

答案 2 :(得分:2)

虽然Alexander Blunck的答案是合理的,但对于早期版本的iOS(例如3.2),以下行会出错,这不是你想要的。

window.rootViewController = viewController;

如果您打算使用Steffen的代码(ugh),那么您可能需要添加一个方法来直接设置ui视图控制器,而不是假设它可以通过UIApplication获取。