Search code examples
xcodeswiftgame-centermultiplayer

Why doesn't this function get called?


Im following the Ray Wenderlich tutorial and converted most of the code to Swift. Im on part two but this function never gets called. I followed the tutoral but im not sure where to call it. I dont think its a delegate function also so I know that im supposed to call it somewhere but not sure where. Has anyone ever tried this tutorial and got it to work on Swift? Thanks!

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

Solution

  • The delegate method match:didReceiveData:fromPlayer: has been deprecated. Use match:didReceiveData:fromRemotePlayer: instead.

    Make sure you set the delegate, doing something like:

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

    That is the viewController:didFindMatch delegate for GKMatchmakerViewControllerDelegate. You need to delegate from both GKMatchmakerViewControllerDelegate and GKMatchDelegate.

    Yeah, I know this is an old thread, but the OP hasn't had any success yet.