GameCenter Multiplayer Stuck on "Starting Game..."
Asked Answered
D

1

9

I am currently working on my game and I have decided to enable multiplayer via GameCenter in the Game to allow users to play their friend. I have followed a tutorial by RayWinderLinch, but ran into a problem.

My problem is that when I load up the GKMatchMakingViewController and hit the big Play Now button on both devices it will find each other (which is meant to happen) and under the set game center user name it will say Ready.

This means that GameCenter has found each player and is ready to start the match which it should, but in my case the match never begins. It is stuck on a loop that says Starting Game... and nothing happens. It appears that the

func matchmakerViewController(viewController: GKMatchmakerViewController!, didFindMatch theMatch: GKMatch!)

and the

func match(theMatch: GKMatch!, player playerID: String!, didChangeState state: GKPlayerConnectionState)

method's are never ran. I am completely lost on what is going on. I have tried this many times over and over to fix the problem but nothing worked. I will attach an image that show's the screen of the application where my problem persists and I will also attach the code I am using.

I am using a framework based of of the GameKitHelper.h In the mentioned tutorial above. It is written in swift and is called GCHelper

Code

The code for GCHelper can be found using the GitHub link mention earlier

I have cut out code that is unnecessary for this problem

 class GameScene : SKScene, GameKitHelper, MultiplayerNetworkingProtocol {

     override func didMoveToView () {

          GCHelper().authenticateLocalUser() //Authenticate GameCenter User
          println("\n \n \n Authenticating local user \n \n \n")

     }

      func startMultiplayer () {

           var vc = self.view?.window?.rootViewController
           GameKitHelper().findMatchWithMinPlayers(2, maxPlayers: 2, viewController: vc!, delegate: self); //Find match and load GKMatchMakerViewController

      }

       func matchStarted() {

           //Delegate method
           println("match started")

       }

       func matchEnded() {

           //Delegate method
           println("match ended")

       }

        func match(match: GKMatch, didReceiveData: NSData, fromPlayer: String){
        //Delegate Method
        println("Did receive data")

}


     override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {

         for touch in (touches as! Set<UITouch>) {
        let location = touch.locationInNode(self)


        if self.nodeAtPoint(location) == multiplayer //SKSpriteNode {

             //User clicked on multiplayer button, launch multiplayer now!
             println("Loading multiplayer")
             startMultiplayer()
      }

 }

Image

Game center finds each player and is ready to start the match but then it get's stuck on Starting Game...

UPDATE

I have noticed that when I test using my iPhone and the simulator, on the iPhone the status will go from Ready to Disconnected but on the simulator the status is still Ready and then I will get the following message in the console for the iPhone

Warning matchmakerViewController:didFindMatch: delegate method not implemented`

Even though it is implemented in the GCHelper.swift file. This does not happen when I test on my iPhone and iPad Mini it just keeps on saying Starting Game...

Any help will be appreciated.

Dextrogyrate answered 29/7, 2015 at 11:40 Comment(7)
Are you setting the Game Center delegates to self/controller? I found Game Center rather buggy on the simulator, so I always use real devices for testingBans
I don't have a clear understanding of what you mentioned, I also tried testing on real devices tooDextrogyrate
You need to implement the didFindMatch - in that method you can dismiss the matchmaker viewcontrollerBans
It is implemented in the GCHelper class as mentioned in the postDextrogyrate
But in your startMultiplayer() your setting the delegate to self - so it expects that the methods are implemented in your class, not the GCHelper.Bans
The bounty expried yesterday I believe but thank you for your answerDextrogyrate
You are welcome. It was an educative research. Yet it appears, according to time stamps, that I answered 2 days ago...Decoupage
D
1

Prerequisites

  • Both players must be in the same environment (Sandbox for testing)
  • The authenticationChanged in GCHelper.swift must not be private. You may have to remove that keyword.

Sandbox Player

There are a few delegates involved, and in your example, there are a few competing protocols. My recommendation is to create a new App using minimalistic code to track down the startMultiplayer issue.

Gamekit Multi Player Step by Step Tutorial using GCHelper

Create a new project (Xcode > File > New > Project... > Single View Application > ... > Create) using the very same Product Name & Organization Name as your game, so that it matches both App Bundle Identifier and iTunes Game Center parameters. This will allow you to run tests without overhead.

Use this Podfile:

platform :ios, '8.0'

use_frameworks!

target 'SO-31699439' do
pod 'GCHelper'
end

Use a GCHelperDelegate

Create a UIViewController with just the bare minimum (a Start Multiplayer button), and connect it to this action:

@IBAction func startMultiplayerAction(_ sender: AnyObject) {
    GCHelper.sharedInstance.findMatchWithMinPlayers(
        2,
        maxPlayers: 2,
        viewController: self,
        delegate: self);
}

Here is the crux: the delegate you pass must adopt GCHelperDelegate. It does not have to be the same class, but in your example above it is, and the present rule was not respected. For this example, ViewController adopts GCHelperDelegate:

import UIKit
import GCHelper
import GameKit

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        GCHelper.sharedInstance.authenticateLocalUser()
    }
}

Implement required GCHelperDelegate methods in an extension

Since ViewController adopts GCHelperDelegate, the three methods below must be in that same class, and will be invoked:

extension ViewController: GCHelperDelegate {
    func matchStarted() {
        print("matchStarted")
    }

    func match(_ match: GKMatch, didReceiveData: Data, fromPlayer: String) {
        print("match:\(match) didReceiveData: fromPlayer:\(fromPlayer)")
    }

    func matchEnded() {
        print("matchEnded")
    }
}

Execution

Tested: built, linked, ran, successful match.

Launch app, tap Start Multiplayer button, tap Play Now on both devices (or iPhone Simulator + real device).

Play now

Log:

Authenticating local user...
Authentication changed: player not authenticated

Ready to start match!
Found player: SandboxPlayer
matchStarted

► Find this solution on GitHub and additional details on Swift Recipes.

Decoupage answered 27/8, 2015 at 22:15 Comment(1)
Hey, Thanks for your responce! But when I tried to install the pod file I got a message from terminal saying [!] Unable to find a target named SO-31699439 So I am wondering if there's anything wrong I am doing, I copied the exact code from the podfile above. ThanksDextrogyrate

© 2022 - 2024 — McMap. All rights reserved.