为什么不调用此函数?

Why doesn't this function get called?

我遵循 Ray Wenderlich 教程并将大部分代码转换为 Swift。我在第二部分,但这个函数永远不会被调用。我遵循了教程,但我不确定在哪里调用它。我不认为它也是一个委托函数,所以我知道我应该在某个地方调用它但不确定在哪里。有没有人试过本教程并让它在 Swift 上运行?谢谢!

继承人link:http://www.raywenderlich.com/60998/game-center-tutorial-how-to-make-a-simple-multiplayer-game-with-sprite-kit-part-2

func match(theMatch: GKMatch, didReceiveData data: NSData, fromPlayer playerID: String) {


    let message = UnsafePointer<Message>(data.bytes).memory

    if(message.messageType == MessageType.kMessageTypeRandomNumber) {
        let messageRandomNumber = UnsafePointer<MessageRandomNumber>(data.bytes).memory

        println("Received random number: \(messageRandomNumber.randomNumber)")

        var tie = false

        if(messageRandomNumber.randomNumber == _ourRandomNumber) {
            println("Tie")
            tie = true
            _ourRandomNumber = arc4random()
            self.sendRandomNumber()
        }
        else {
            var dictionary = ["\(playerIDKey)":"\(playerID)", "\(randomNumberKey)":"\(messageRandomNumber.randomNumber)"]
            self.processReceivedRandomNumber(dictionary)
        }

        if(_receivedAllRandomNumbers) {
            _isPlayer1 = self.isLocalPlayerPlayer1()
        }

        if(!tie && _receivedAllRandomNumbers) {
            if(_gameState == GameState.kGameStateWaitingForRandomNumber) {
                _gameState = GameState.kGameStateWaitingForStart
            }
            self.tryStartGame()
        }
    }
    else if(message.messageType == MessageType.kMessageTypeGameBegin) {
        println("Begin game message received")
        _gameState = GameState.kGameStateActive
        self.delegate?.setCurrentPlayerIndex(self.indexForLocalPlayer())
    }
    else if(message.messageType == MessageType.kMessageTypeMove) {
        println("Move message received")
        let messageMove = UnsafePointer<MessageMove>(data.bytes).memory
        self.delegate?.movePlayerAtIndex(self.indexForPlayerWithId(playerIDKey))
    }
    else if(message.messageType == MessageType.kMessageTypeGameOver) {
        println("Game over message received")
        let messageGameOver = UnsafePointer<MessageGameOver>(data.bytes).memory
        self.delegate?.gameOver(messageGameOver.player1Won)
    }
}

match:didReceiveData:fromPlayer:是一个GKMatchDelegate可选方法,当比赛收到玩家发送的数据时会触发。

所以请不要尝试直接调用它。当从玩家接收到数据时,GameKit 将调用它。

@protocol GKMatchDelegate <NSObject>
@optional

// The match received data sent from the player.
- (void)match:(GKMatch *)match didReceiveData:(NSData *)data fromRemotePlayer:(GKPlayer *)player NS_AVAILABLE(10_10, 8_0);
- (void)match:(GKMatch *)match didReceiveData:(NSData *)data fromPlayer:(NSString *)playerID NS_DEPRECATED(10_8, 10_10, 4_1, 8_0, "use match:didReceiveData:fromRemotePlayer:");

@end;

这也在 GameKitHelper.h class 中声明为 GameKitHelperDelegate

@protocol GameKitHelperDelegate

- (void)match:(GKMatch *)match didReceiveData:(NSData *)data
   fromPlayer:(NSString *)playerID;

@end

当比赛接收到玩家发送的数据时,此委托方法从以下方法调用

#pragma mark GKMatchDelegate

// The match received data sent from the player.
- (void)match:(GKMatch *)match didReceiveData:(NSData *)data fromPlayer:(NSString *)playerID {
    if (_match != match) return;

    [_delegate match:match didReceiveData:data fromPlayer:playerID];
}

委托方法 match:didReceiveData:fromPlayer: 已被弃用。请改用 match:didReceiveData:fromRemotePlayer:

确保设置代理,执行如下操作:

func matchmakerViewController(viewController: GKMatchmakerViewController, didFindMatch match: GKMatch) {
    theMatch = match
    match.delegate = self
    ...
}

那是 GKMatchmakerViewControllerDelegateviewController:didFindMatch 代表。 您需要同时委托 GKMatchmakerViewControllerDelegateGKMatchDelegate.

是的,我知道这是一个旧线程,但 OP 尚未取得任何成功。