Impact

This class implements Game Center Achievements and Leaderboards. Note that you have to set up the achievements and leaderboards in iTunesConnect first – with the correct bundle identifier as specified in your in Info.plist.

Synopsis

// At game start
var gameCenter = new Ejecta.GameCenter();
gameCenter.softAuthenticate(function(error){
	if( error ) {
		console.log( 'Auth failed' );
	}
	else {
		console.log( 'Auth successful' );
	}
});

// ... later in your game
if( gameCenter.authed ) {
	
	// Report Achievment 'didsomething' with 100% completition
	gameCenter.reportAchievement( 'didsomething', 100, function( error) {
		if( error ) {
			console.log( 'Reporting the achievement failed' );
		}
		else {
			console.log( 'Achievement successfully reported' );
		}
	});
	
	// Report highscore 742 for the 'normal' list
	gameCenter.reportScore( 'normal', 742, function( error ) {
		if( error ) {
			console.log( 'Reporting the score failed' );
		}
		else {
			console.log( 'Score successfully reported' );
		}
	});
}

Class Reference

.authenticate( [callback] )

This will authenticate the player or pop up the GameCenter login form. This function should be called when the player is not already authenticated and presses a GameCenter button on the Game's title screen.

The optional callback is called with an error boolean, indicating if the login failed.

.softAuthenticate( [callback] )

Try to authenticate the local player. This should be called once at game start.

If the previous auth was successful (or never tried for this game), this function will attempt to automatically log you in; if the previous auth wasn't successful, this function will do nothing.

The idea is that you can call this method as soon as the game starts and have an extra button on your title screen that calls the normal .authenticate( [callback] ) method. People who don't want to use GameCenter, will not be annoyed by a login screen at every game start and everyone who previously logged in, will be logged in automatically.

.loadFriends( callback(error, players[]){} )

Loads all friends and calls the callback with an error string or an array of players, each with alias, displayName, playerId, isFriend properties.

.loadScores( category, rangeStart, rangeEnd, callback(error, scores[]){} )

Loads scores from the given category and range and calls the callback with an error string or an array of
scores, each with category, player, date, formattedValue, value, rank properties.

.getLocalPlayerInfo()

Returns the local player with alias, displayName, playerId, isFriend properties

.reportAchievement( name, percentage, [callback] )

Report the achievement with the given name and absolute percentage. The optional callback is called with an error boolean, indicating if reporting failed.

.reportAchievementAdd( name, addPercentage, [callback] )

Report the achievement with the given name and relative percentage. The optional callback is called with an error boolean, indicating if reporting failed.

If you have a "Collect 10 coins" achievement, you can call this function for each coin collected with a percentage of 10, and the percentage will add up for each call.

.showAchievements()

Opens Game Center and displays the achievements for your game.

.reportScore( listName, score, [callback] )

Report a highscore for the list with the given name. The optional callback is called with an error boolean, indicating if reporting failed.

.showLeaderboard( listName )

Opens Game Center and displays the leaderboard with the given name.

.authed

Boolean, indicating if the local player is authed. Only true after .authenticate() finished successfully.