Created
March 29, 2014 13:09
-
-
Save bulkan/9854219 to your computer and use it in GitHub Desktop.
Sample code for CocoonJS submit to achievement completion to game center
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE HTML> | |
<html> | |
<head> | |
<script src="assets/CocoonJSExtensions/CocoonJS.js"></script> | |
<script src="assets/CocoonJSExtensions/CocoonJS_App.js"></script> | |
<script src="assets/CocoonJSExtensions/CocoonJS_App_ForCocoonJS.js"></script> | |
<script src="assets/CocoonJSExtensions/CocoonJS_Social.js" type="text/javascript"></script> | |
<script src="assets/CocoonJSExtensions/CocoonJS_Social_GameCenter.js" type="text/javascript"></script> | |
<script type="text/javascript"> | |
var canvas = document.createElement("canvas"); | |
canvas.setAttribute("screencanvas", "1"); //performance improvement hint in cocoonjs | |
canvas.width = window.innerWidth ; | |
canvas.height = window.innerHeight; | |
var ctx = canvas.getContext("2d"); | |
document.body.appendChild(canvas); | |
var gc = CocoonJS.Social.GameCenter | |
, socialService = gc.getSocialInterface() | |
, loggedIn = false; | |
if (socialService) { | |
socialService.onLoginStatusChanged.addEventListener(function(_loggedIn) { | |
loggedIn = _loggedIn; | |
if (_loggedIn) { | |
console.log("Logged into social service"); | |
gc.submitAchievements([{identifier: "test_achievement", showsCompletionBanner: true, percentComplete:100}], function(error){ | |
if (error) { | |
return console.error("Error submittingAchievemnt: " + error); | |
} | |
}) | |
}) | |
socialService.login(function(loggedIn, error){ | |
if (!loggedIn || error) { | |
console.error("Login failed: " + JSON.stringify(error)); | |
//Tell the user that Game Center is Disabled | |
if (error.code == 2) { | |
// go to gamecenter app | |
CocoonJS.App.onMessageBoxConfirmed.addEventListenerOnce(function(){ | |
CocoonJS.App.openURL("gamecenter:"); | |
}); | |
CocoonJS.App.showMessageBox("Game Center Not Signed In", "Please sign into Game Center app to enable leaderboards and achievements", "OK", "Later"); | |
} | |
} | |
}); | |
} | |
</script> | |
</head> | |
<body></body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment