我有一个永远不会被调用的委托方法......为什么?

时间:2015-08-08 01:52:49

标签: ios swift game-center

这是未被调用的委托方法。我尝试了一切,但由于某些原因它永远不会被调用。我试图在Swift中使用游戏中心制作多人游戏。我在MultiplayerNetworking.swift文件中有一个函数,我必须在GameScene.swift的委托函数中调用它。有人能帮我吗?谢谢!

//GameScene.swift

/* For protocol GKMatchDelegate */

func match(match: GKMatch, didReceiveData data: NSData, fromRemotePlayer playerID: GKPlayer) {
    println("calllittttttt")
    if _match != match {
        return


    }


    MultiplayerNetworking().match(match, didReceiveData: data, fromRemotPlayer: playerID)

}


//MultiplayerNetworking.Swift

   func match(match:GKMatch, didReceiveData data: NSData!, fromRemotPlayer 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(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)
    }
}

1 个答案:

答案 0 :(得分:0)

很可能你的代表将是零。你的代表,看看。