In this tutorial, youâll learn about authentication with Game Center and how its turn-based mechanics work. In the end, youâll have the foundations of how to integrate a multiplayer game with GameCenter.
Hello, Iâm sorry this happened. I assume itâs crashing because the notification is fired before setUpScene(in:) is called. This would result in the onlineButton being nil. To fix this youâll need to change:
I am trying to use some of the ideas here for another 2-player game, which is in many ways similar to chess but much simpler. However, there are also two âfactionsâ and one of the always starts (similar to âwhiteâ in chess). I would like to assign players to these factions before the game (or perhaps give them the possibility to choose). What do you think would be a good way of doing this? I guess what is not very clear to me from your tutorial is how you know that a new game has started, rather than it just being your turn
Sorry for the delay in responding. Iâm glad you enjoyed the tutorial!
If youâd like to determine when there is a new game you can check to see if the game model has any information. For instance, in the tutorial inside MenuScene.swift the loadAndDisplay(match: GKTurnBasedMatch) attempts to load the gameâs data for a match. If there isnât any data returned from Game Center then a new game is created.
For your question when you create a new game part of your logic can include setting a state in the model where your game scene would recognize itâs in the âselect a factionâ state and ask the player to choose. You can also just assign a player at this point if youâd rather not have the players choose.
I hope that helps. Let me know if I can elaborate more, or answer anything else!
Hi. I tried to run this on my iPad Air 2 and it crashed with the following error when I pressed the load Game button. Nine Knights[664:75183] *** Terminating app due to uncaught exception âNSInvalidArgumentExceptionâ, reason: âInvalid size specified: {768, -70.9119873046875}â
The game ran fine on my iPhone. I made the following change to prevent the crash in iPad Air 2.
Class: GameScene
replace:
let skySize = CGSize(width: viewWidth, height: viewHeight - groundNode.position.y)
with:
let adjustedHeight = max(0, viewHeight - groundNode.position.y)
let skySize = CGSize(width: viewWidth, height: adjustedHeight)
This fixes the crash, but to make the screen look good on both an iPad and iPhone make additional changes:
Thanks for point this issue out! I went ahead and updated the sample project based on this. I also made the board smaller on iPad so the layout wasnât overlapping.