如何使用gamecenter制作一个简单多人游戏教程第一部分_第1页
如何使用gamecenter制作一个简单多人游戏教程第一部分_第2页
如何使用gamecenter制作一个简单多人游戏教程第一部分_第3页
如何使用gamecenter制作一个简单多人游戏教程第一部分_第4页
如何使用gamecenter制作一个简单多人游戏教程第一部分_第5页
已阅读5页,还剩17页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

如何使用GameCenter制作一个简单的多属于:子龙山人首发于:泰然整理:滔小免责申明(必读!):本博客提供的所有的翻译原稿均来自于互联网,仅何纠纷,均与本博客所有人、该翻译稿之人无任何关系。谢谢合作!原文地址我正在实验以一种新的方式来撰写--通过采纳的建议来写!决定下一篇是什么。(当然,在原作者上面,我这里没有啦的多人---现在满足的要求!来做一个简单的2人联机玩的 帐号,只需要提供不同的邮件地址就ok)Getting上 截屏友和你一起玩,或者使用mataking来随机查找线上的玩家。在iTunesConnect里面你的创建和设置App然后进到iOSProvisioningPortal。一旦完成之后,点击Submit。然后打开CatRaceXcode工程,选择3下面,你可以通过ITunesConnect你的应用程序并激活GameCenter。在ItunesConnect中你的程接下来,登录 iTunesConnect并为你的应用创建一个新的(entry)然后点左上角的蓝色的“AddNewApp”按钮。很happy了。点右上角的蓝色的“ManageGameCenter”按钮,然后点“Enable”按钮,顺便提一下,在“ManagerGameCenter”部分,你可能注意到了们,但是,如果对此感的话,你可以在我即将的书里找到。认证本地的用户:策略那么会收到 eBack!”消息。否则,它会要求玩家输入用户名和 pletionHandler就可以了。你可以选择性地传入一个ios3.0之前是不能使用的)“authenticationchanged”notification。当单例对象创建的时候,它会“authentication,“authentication打开CatRace项目,点File\New\NewFile,然后选择 Touch\Objective-Cclass,再点Next。选择NSObject作为基类,再点Next,把它命名为GCHelper,然后点Finish。#import<GameKit/GameKit.h>@interfaceGCHelper:{BOOLgameCenterAvailable;BOOLuserAuthenticated;}@property(assign,readonly)BOOL+(GCHelper- gamecenter法(这个方在app启动的时候被调用)#pragmamarkInitializationstaticGCHelper*sharedHelper=+(GCHelper*){if(!sharedHelper)sharedHelper=[[GCHelperalloc]}return}-(BOOL)isGameCenterAvailable-(BOOL)isGameCenterAvailable//checkforpresenceofGKLocalPlayerClassgcClass=//checkifthedeviceisrunningiOS4.1orlaterNSString*reqSysVer=@"4.1";NSString*currSysVer=[[UIDevicecurrentDevice]systemVersion];BOOLosVersionSupported=([currSysVercompare:reqSysVeroptions:NSNumericSearch]!=NSOrderedAscending);return(gcClass&&}GameKitProgrammingGuidecopy它用来检测当前设备是否支持gamecenter。-(id)initif((self=[superinit]))gameCenterAvailable=[selfisGameCenterAvailable];if(gameCenterAvailable){NSNotificationCenter*nc=[NSNotificationCenterdefaultCenter];[ncaddObserver:self}}return}-(void)authenticationChangedif([GKLocalPlayerlocalPlayer].isAuthenticated&&{NSLog(@"Authenticationchanged:playerauthenticated.");userAuthenticated=TRUE;}elseif(![GKLocalPlayerlocalPlayer].isAuthenticated&&userAuthenticated){NSLog(@"Authenticationchanged:playernotauthenticated");userAuthenticated=FALSE;}}册“authenticationchanged”notification。(这是非常经典的观察者模#pragma#pragmamarkUser- {if(!gameCenterAvailable)NSLog(@"Authenticatinglocalif([GKLocalPlayerlocalPlayer].authenticated=={[[GKLocalPlayer }else这里调用前面提到过的a pletionHandler来认证用 没有必要再写一个handler了。用它们了!回到AppDelegate.,然后做如下更改://Atthetopofthe//Atthetopofthe#import#import//ofapplicationDidFinishLaunching,right//thelastlinethatcalls[[GCHelpersharedInstance] 这里创建GCHelper的单例(它的init方法里面了“authenticationchanged”通告)并调用authenticaocalUser方法。GameKitframework先点工程文件,然后选择BuildPhases,展开“LinkBinarywithLibraries”,再点”+“号来添加相应的framework。选择GameKit.frameworkAddtypeRequired,Mataker,Mataker,给我找个对手吧程来主动查找,另一种是使用内置的mataking接口。在这篇中,使用内置的mataking接口。当你想要寻找一个对示一个GKMatakerViewController就ok了。 //Addtotopoffile//Addtotopoffile(void)match:(GKMatch*)matchdidReceiveData:(NSData*)datafromPlayer:(NSString*)playerID;//Modify@interfacelinetosupportprotocolsas@interfaceGCHelper:NSObject<GKMat akerViewControllerDelegate,GKMatchDelegate>{//AddinsideGKMatch*match;BOOLid<GCHelperDelegate>//Addafter@property(retain)UIViewController*presentingViewController;@property(retain)GKMatch*match;@property(assign)id<GCHelperDelegate>--(void)findMatchWithMinPlayers:(int)minPlayersmaxPlayers:(int)maxPlviewController:(UIViewControllerGCHelperDelegatematch实现该协议。在本例中,cocos2d的layer将会实现此协议。同时,GCHelper对象实现了两个协议。第一个是mat aker进行玩家查个就是当数据到达或者连接状态改变的时候,GameCenter会通知GCHelper对象。个对象将会用来显示mataker用户界面一个match对象的,match是否开始的标记以及一个。//Attopof//Attopof@synthesizematch;@synthesize//Addnewmethod,rightafter -(void)findMatchWithMinPlayers:(int)minPlayersmaxPlayers:(int)maxPldelegate:(id<GCHelperDelegate>)theDelegate{if(!gameCenterAvailable)matchStartedmatchStarted=self.match=nil;self.presentingViewController=viewController;delegate=theDelegate;[presentingViewControllerGKMatchRequest*request=[[[GKMatchRequestalloc]init]request.minPlayers=request.maxPlayers= akerViewController*mmvcakerViewControlleralloc]initWithMatchRequest:request]akerDelegate=[presentingViewControllerpresentModalViewController:mmvc}它首先初始化match为未开始状态,并把match对象设置为nil。并且,存视图控制器(比如:GKMatakerViewController已经显示出来了)。何数量。但是,本只需要设置最小和最大都为2就可以了。接下来,使用给定的request来创建一个#pragmamark //Theuserhascancelled -(void)mat ler*)viewController{[presentingViewController}// akinghasfailedwithan-(void)mat akerViewController*)viewControllerdidFailWithError:(NSError*)error{[presentingViewControllerdismissModalViewControllerAnimated:YES];NSLog(@"Errorfindingmatch:%@",error.localizedDescription);} er-to-peermatchhasbeenfound,thegameshould-(void)mat akerViewController*)viewControllerdidFindMatch:(GKMatch*)theMatch{[presentingViewControllerdismissModalViewControllerAnimated:YES];self.match=theMatch;match.delegate=if(!matchStarted&&match.expectedPlayerCount=={NSLog(@"Readytostart}}闭mat aker视图。delegateGCHelper对象。这样的话,当有新的数据到达,或者连接状态改变的话,GCHelper对象就会得到通知。多少个玩家才能完成连接,这个数目由“expectedPlayerCount”来定。#pragma#pragmamark//Thematchreceiveddatasentfromthe(void)match:(GKMatch*)theMatchdidReceiveData:(NSData*)datafromPlayer:(NSString*)playerID{if(match!=theMatch)[delegatematch:theMatchdidReceiveData:data}//Theplayerstatechanged(eg.connectedor(void)match:(GKMatch*)theMatchplayer:(NSString*)playerIDdidChangeState:(GKPlayerConnectionState)state{if(match!=theMatch)switch(state)case//handleanewplayerconnection.NSLog(@"Playerconnected!");if(!matchStarted&&theMatch.expectedPlayerCount=={NSLog(@"Readytostart}case//aplayerjustdisconnected.matchStarted=NO;}}//Thematchwasunabletoconnectwiththeplayerduetoan-(void)match:(GKMatch*)theMatchconnectionWithPlayerFailed:(NSString*)playerIDwithError:(NSError*)error{if(match!=theMatch)NSLog(@"Failedtoconnecttoplayerwitherror:%@",error.localizedDmatchStarted=NO;}//Thematchwasunabletobeestablishedwithanyplayersduetoan-(void)match:(GKMatch*)theMatchdidFailWithError:(NSError{if(match!=theMatch)NSLog(@"Matchfailedwitherror:%@",error.localizedDescription);matchStarted=NO;[delegate}时候被调用的。这个方法只是简单的把这些数据再转发给它的类。(这Formatch:player:didChangState这个方法,是当有玩家接入的时候,你需已经结束了,同时通知delegate对象。////Addtotopoffile#import"GCHelper.h"//Mark@interfaceasimplementingGCHelperDelegate oWorldLayer:CCLayer<GCHelperDelegate>//Addtotopoffile//Addtotopoffile#import//Addtobottomofinitmethod,rightafterAppDelegate*delegate=(AppDelegate*)[UIApplicationsharedApplica[[GCHelpersharedInstance]findMatchWithMinPlayers:2maxPlayers:2viewController:delegate.viewControllerdelegate:self];//Addnewmethodstobottomoffile#pragmamarkGCHelperDelegate-{CCLOG(@"Match}}{CCLOG(@"Match}(void)match:(GKMatch*)matchdidReceiveData:(NSData*)datafromPlayer:(NSString*)playerID{CCLOG(@"Received这里最重要的部分就是init方法。它从AppDelegate那里得到一个创建一个GCHelper对象来查找一个match。一个RootViewController的属性,因此你必须手动添加一个。跳转到AppDelegate.h文件,并添加下面的代码:@property@property(nonatomic,retain)RootViewController@synthesize@synthesize面,一个运行在上面。在两个设备上都点击“PlayNow

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论