为什么我的委托函数didReceiveData在Swift中被调用?

时间:2015-07-30 16:42:35

标签: ios xcode swift game-center

我正在制作多人实时游戏中心游戏,我正在使用本教程http://www.raywenderlich.com/60998/game-center-tutorial-how-to-make-a-simple-multiplayer-game-with-sprite-kit-part-2。我一直到控制台中的部分说“匹配已成功启动”。应该调用的下一个委托函数是didReceiveData函数,但它永远不会被调用。我完全按照教程中的说法完成了它,但它对我不起作用。

    //MultiplayerNetworking.swift
  func match(match: GKMatch!, didReceiveData data: NSData!, fromRemotePlayer playerID: GKPlayer!) {



    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)
    }
}


//GameScene.swift


protocol GameKitHelperDelegate {
func matchEnded()
func matchStartedNow()
func match(match: GKMatch!, didReceiveData data: NSData!, fromRemotePlayer playerID: GKPlayer!)

func moveForward()

}

func match(match: GKMatch!, didReceiveData data: NSData!, fromRemotePlayer playerID: GKPlayer!) {



    if(_match != match) {

        println("works")
        return
    }
    _delegate?.match(match, didReceiveData: data, fromRemotePlayer: playerID)

    println("didnt work")
        }

1 个答案:

答案 0 :(得分:1)

您使用的是哪个iOS版本? 因为它在文档中清楚地说明了以下方法:

- (void)match:(GKMatch *)match didReceiveData:(NSData *)data fromRemotePlayer:(GKPlayer *)player

适用于iOS8及以上版本。

以下方法:

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

在iOS8中已弃用。