如何在Swift中实现这个基于Objec-C回合制游戏中心系统?

时间:2015-10-14 19:56:08

标签: ios swift game-center

我遵循本教程,并且我很难完成它,因为Swift的回合制游戏没有任何内容。

我可以在同一视图中成功与两个用户进行匹配。我现在只需要设置一个转弯系统,并允许用户每回合发送一个文本输入。但是,我完全迷失了如何做到这一点,因为它在Objec-C中完全不同。任何有关这方面的指导都将非常感激。

目前正在使用GitHub的Game Center助手。目前的代码:

func findGameButton(sender: UIButton) {   // Run the following code when Find Game is hit.

    EasyGameCenter.findMatchWithMinPlayers(2, maxPlayers: 2) // Matchmaking

}


 /**
    Match Recept Data, Delegate Func of Easy Game Center
    */
    func easyGameCenterMatchRecept(match: GKMatch, didReceiveData Data: NSData, fromPlayer playerID: String) {
        print("\n[MultiPlayerActions] Recept Data from Match !")
    }
    /**
    Match End / Error (No NetWork example), Delegate Func of Easy Game Center
    */
    func easyGameCenterMatchEnded() {
        /// When the game finished
        print("\n[MultiPlayerActions] Match Ended !")
    }
    /**
    Match Cancel, Delegate Func of Easy Game Center
    */
    func easyGameCenterMatchCancel() {
        /// if player cancel the game
        print("\n[MultiPlayerActions] Match cancel")
    }

这是基于回合的系统和发送数据在Objec-C中的完成方式。如何在Swift中完成?可以做得更好吗?很想看到一个真正的回合制游戏项目写的Swift,但没有可用的。

- (IBAction)sendTurn:(id)sender {

    GKTurnBasedMatch *currentMatch = // Set up currentMatch variable by retrieving the match from GameKitHelper singleton, only display one match at a time - keep track of this one match in our currrent match variable.
    [[GameKitHelper sharedInstance] currentMatch];
    NSString *newStoryString;
    if ([inputText.text length] > 250) {
        newStoryString = [inputText.text substringToIndex:249]; // Check if length of string in text is too long. If more than 250 characters we cut it off using the substringToIndex call. If not, we just pass the string into our variable.
    } else {
        newStoryString = inputText.text;
    }
    NSString *sendString = [NSString stringWithFormat:@"%@ %@",
                            mainTextController.text, newStoryString];
    NSData *data = // Created by combining the string thats in mainTextController with the string we just created.
    [sendString dataUsingEncoding:NSUTF8StringEncoding ];
    //cachedOldStory used to rollback in case of failing to send out data
    NSString *cachedOldStory = [mainTextController.text copy];
    mainTextController.text = sendString;

    inputText.text = @"";
    characterCount.text = @"250";
    characterCount.textColor = [UIColor blackColor];

    NSUInteger currentIndex = [currentMatch.participants // For every turn sent, information about the next person in turn roatation is
                               indexOfObject:currentMatch.currentParticipant];

    GKTurnBasedParticipant *nextParticipant;
    for (int i = 0; i < [currentMatch.participants count]; i++)
    {

    NSUInteger nextIndex = (currentIndex +1 + i) %
                        [currentMatch.participants count];
    nextParticipant = [currentMatch.participants objectAtIndex:nextIndex];
            if (nextParticipant.matchOutcome != GKTurnBasedMatchOutcomeQuit) {
            break;
        }
    }

    NSArray *nextParticipants = [[NSArray alloc] initWithObjects:nextParticipant, nil];
    [currentMatch endTurnWithNextParticipants:nextParticipants
                                  turnTimeout:GKExchangeTimeoutDefault
                                    matchData:data
                            completionHandler:^(NSError *error)
     {
         if (error)
         {
             NSLog(@"%@", error);
           //  statusLabel.text = @"Error, check Network and Game Center. Please try again.";
             mainTextController.text = cachedOldStory;
         }
         else
         {
             // statusLabel.text = @"Your turn is over.";
             inputText.enabled = NO;
         }
     }
     ];

    NSLog(@"Send Turn, %@, %@", data, nextParticipant);

}

0 个答案:

没有答案