适用于iOS 5/6的在线实时多人游戏

pla*_*res 3 iphone cocoa cocoa-touch

我知道已有一些关于这个主题的帖子,但我找不到我需要的答案,我不知道从哪里开始.

我想为iPhone创建一个在线多人游戏,玩家可以通过互联网一起玩.例如,2玩家赛车游戏,一旦2个玩家匹配并连接,他们就可以实时控制他们的汽车彼此.比如马里奥车.

我知道游戏套件确实如此,但只能通过蓝牙或同一个wifi网络.我希望这可以通过互联网(仅限wifi),玩家可以在世界各地互相对战.

我知道已经有一些框架可以做到这一点.但它们成本高昂,取决于连接数量.有没有便宜或敢说,免费的方式这样做?喜欢游戏套件做匹配然后连接和发送数据是以其他方式完成的吗?就像拥有iPhone主机游戏一样?而不是拥有一个专用的服务器.我没有预算,也没有创建专用服务器的知识和经验.

匹配很简单.有30个级别,任何想要玩相同级别的2个玩家都匹配.

欢迎任何链接或书籍推荐.我的网络知识非常有限,不知道从哪里开始.

我可以阅读和学习技术,即使它们是技术性的,但我需要合适的资源才能让我开始

提前致谢.

Cha*_*son 5

您实际上可以使用Game Kit API完成您正在寻找的内容.基本上,您使用GKMatchMakerViewController创建匹配.为了进行匹配,您使用GKMatchMakerViewController开始寻找其他玩家,一旦找到其他玩家,它就会通知GKMatchMakerViewControllerDelegate已找到匹配并将传递GKMatch对象.然后,您需要一个实现GKMatchDelegate协议的对象来处理实际数据.您将委托对象设置为您传递的GKMatch的委托,然后使用GKMatchDelegate协议中的方法,例如 - match:didReceiveData:fromPlayer:和GKMatch中的方法来发送数据.

下面是一些示例代码来帮助解释.这只是一个基本要素,你当然需要实现你的游戏玩法,以及一些错误处理.

此外,您可以在这四个链接中找到所需的文档

GKMatchMakerViewController GKMatchMakerViewControllerDelegate GKMatch GKMatchDelegate

- (void)match:(GKMatch *)match didReceiveData:(NSData *)data fromPlayer:(NSString *)playerID{
    if(matchStarted){
        Packet *msg = (Packet *)[data bytes];
       //do whatever you want with the data received from other people
    }
}

-(void)sendPosition{
    //call this to update the other players devices (should be self explanatory)
    NSError *error;
    Packet msg;
    //Here the msg object is actually a typedefed struct name Packet. I use this to send and receive data
    NSData *packet = [NSData dataWithBytes:&msg length:sizeof(msg)];
    [myMatch sendDataToAllPlayers: packet withDataMode: GKMatchSendDataUnreliable error:&error];
    if (error != nil)
    {
        // handle the error
    }
}

#pragma mark MatchSetup

- (void)matchmakerViewController:(GKMatchmakerViewController *)viewController didFindMatch:(GKMatch *)match{
    [self dismissModalViewControllerAnimated:YES];
    self.myMatch = match; // Use a retaining property to retain the match.
    self.myMatch.delegate = self;
    if (!matchStarted)
    {
        // Insert application-specific code to begin the match.
    }
}
Run Code Online (Sandbox Code Playgroud)