3 navigator.game API Usage
Kevin Lau редактировал(а) эту страницу 2014-02-09 11:05:23 -08:00

Setup

Import include.js and configure the game name:

<script src="http://localhost:5000/static/include.js"></script>
<script type="text/javascript">
    navigator.game.configure('game_name');
</script>

Functions

###authenticate() Authenticates the user. This is already done on page load, so you don't usually have to call this manually.

###playing() Starts playing the game. Needs to be done after authentication and before any other game functions are called.

###getFriends() Returns a promise object that contains a list of friends.

Usage:

navigator.game.getFriends().then(function (data)  {
    // success handler goes here
    if (!data || data.length <= 0) {
        data = "No friends :(";
    }
    alert('Online friends with this game:\n' + data);
}, function (data) {
    // failure handler goes here
})

###getFriendsPlaying() Similar to getFriends(), but only gets the friends playing this game right now.

Usage:

navigator.game.getFriendsPlaying().then(function (data)  {
    // success handler goes here
    if (!data || data.length <= 0) {
        data = "No friends :(";
    }
    alert('Online friends playing this game:\n' + data);
}, function (data) {
    // failure handler goes here
})

###postFriend(friendID, blob) Send a JSON blob (ex. {"msg": "hi"}) to the friend with ID friendID.

###updateScore(board, increment) Update the given board with the latest score increment

###store(type, value) Store a type and its value to local storage.

###retrieve(type) Get the value associated with the type from local storage as a promise object.

Usage:

navigator.game.retrieve(form.rType.value).then(function (data)  {
    // success handler goes here
    alert('Retrieved value:\n' + data);
}, function (data) {
    // failure handler goes here
})