为什么不调用此函数?

时间:2015-07-30 05:41:39

标签: xcode swift game-center multiplayer

我遵循Ray Wenderlich教程并将大部分代码转换为Swift。我在第二部分,但这个功能永远不会被调用。我跟着时态,但我不知道在哪里打电话。我不认为它是一个委托功能,所以我知道我应该把它称之为某处,但不知道在哪里。有没有人试过这个教程并让它在Swift上工作?谢谢!

下面是链接: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)
    }
}

2 个答案:

答案 0 :(得分:1)

委托方法type Closure = Box<for<'a> Fn(&'a i32) -> i32>; fn foo<'a>() { let add_closure = |closure: Closure| { let bar: Closure = Box::new(move |x| { println!("{}", x); closure(x) }); }; add_closure(Box::new(|&x| x + 2)); } fn main() { foo() } 已被弃用。请改用match:didReceiveData:fromPlayer:

确保设置委托,执行以下操作:

match:didReceiveData:fromRemotePlayer:

这是func matchmakerViewController(viewController: GKMatchmakerViewController, didFindMatch match: GKMatch) { theMatch = match match.delegate = self ... } 的{​​{1}}代表。 您需要委派viewController:didFindMatchGKMatchmakerViewControllerDelegate

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

答案 1 :(得分:0)

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类中声明为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];
}