快速入門 – 遊戲 – 文件 – Meta for Developers
Mục lục bài viết
建立遊戲用戶端
應用程式現在已設定完成,您需要開始建立遊戲用戶端。遊戲用戶端必須在根目錄中有一個 index.html
檔案。請先加入下行以匯入即時遊戲 SDK。
<script src="https://connect.facebook.net/en_US/fbinstant.6.2.js"></script>
重要注意事項:
- 請勿下載 SDK 並將其加入組合包,因其在後續步驟中將遭拒絕。
- 此為建置 Facebook 遊戲的新方法,不支援圖形 API。
此 SDK 大量使用 Promise 來進行非同步功能。只有在 FBInstant.initializeAsync()
解析後,您才能夠與載入使用介面互動。
FBInstant.initializeAsync() .then(function() // Start loading game assets here);
我們的遊戲用戶端不會一次完全下載您的組合包(.zip
檔案),而是會在根目錄中搜尋設定檔(fbapp-config.json
)和主檔案(index.html
)。接著開始執行主檔案中的邏輯,然後才開始下載資產。身為開發人員,您可全權控制資產載入的順序和時間。
一旦開始下載初始化遊戲所需的資產,就必須將載入進度告知 SDK,以便向玩家顯示載入圈圈。
var images = ['sprite1', 'sprite2', ...];for (var i=0; i < images.length; i++) { var assetName = images[i]; var progress = ((i+1)/images.length) * 100; game.load.image(assetName); // Informs the SDK of loading progress FBInstant.setLoadingProgress(progress);}// Once all assets are loaded, tells the SDK // to end loading view and start the gameFBInstant.startGameAsync() .then(function() { // Retrieving context and player information can only be done // once startGameAsync() resolves var contextId = FBInstant.context.getID(); var contextType = FBInstant.context.getType(); var playerName = FBInstant.player.getName(); var playerPic = FBInstant.player.getPhoto(); var playerId = FBInstant.player.getID(); // Once startGameAsync() resolves it also means the loading view has // been removed and the user can see the game viewport game.start();});
如需深入瞭解 initializeAsync()
、setLoadingProgress()
和 startGameAsync()
等方法,請參閱 SDK 參考資料。