Adobe AIR & GameCircle: Enabling Whispersync

Introduction

Once you’ve set up your GameCircle Adobe AIR project, you’re ready to add in Whispersync for Games. Whispersync for Games lets you synchronize your players’ game progress to the cloud. With Whispersync for Games enabled, your players can:

  • Uninstall and reinstall their games without losing their progress
  • Retrieve saved game data when they buy a new Fire tablet
  • Synchronize progress across multiple cross-platform devices
  • Revert their game to a previous save in history (up to 5 saves)

The Adobe AIR ANE for GameCircle greatly simplifies the task of enabling Whispersync for Games. This page covers the specific steps you need to take:

  1. Import the Amazon GameCircle SharedObject.
  2. Set up listeners for Whispersync events.

For a more detailed discussion on how Whispersync for Games works, see Plan How to Synchronize Data with Whispersync for Games.

Import SharedObject for Whispersync for Games

The GameCircle ANE synchronizes game data using an interface similar to the flash.net.SharedObject, which is used for storing progress in regular Flash and AIR games. To implement it in your game, replace the flash.net.SharedObject import statement with the Amazon GameCircle version:

import flash.net.SharedObject;
import com.amazon.extensions.android.SharedObject;

The getLocal(), .data, setProperty(), flush(), and setDirty() methods and properties will continue to function just as they do in a regular flash.net.SharedObject.

Listen for WhisperSync for Games Events

The GameCircle AIR ANE fires the following events in response to WhisperSync for Games synchronize requests:

  • WhisperSyncEvent.GAME_DATA_UPLOADED when new data is successfully uploaded
  • WhisperSyncEvent.NEW_GAME_DATA when new data has been synced locally from the cloud

The following code example illustrates how to update a text field on the screen with the value of a SharedObject property (goldStars) in response to a WhisperSync for Games event. In some cases, the data in the cloud may be in conflict with the local data in the SharedObject. In the example, we check for a conflict, and apply the highest value of goldStars as the right value.

Example:

GameCircle.gameCircle.addEventListener(WhisperSyncEvent.NEW_GAME_DATA,  onCloudDataUpdate);
GameCircle.gameCircle.addEventListener(WhisperSyncEvent.GAME_DATA_UPLOADED,  onCloudDataUpdate);

function onCloudDataUpdate(e:WhisperSyncEvent):**void** {
 var mySharedObject:SharedObject=sharedObject.getLocal("mySavedState");
 if  (mySharedObject.isConflicted()){
   var  myData:Object=mySharedObject.data;
   var  cloudData:Object=mySharedObject.getCloudVersionConflictData();
   var  mostStars:Number=Math.max(cloudData.goldStars, myData.goldStars);
   mySharedObject.data.goldStars=mostStars;
   mySharedObject.markConflictResolved();
 }

 // the data update is complete, so our shared object may  have changed...
 var starCount:**int**=mySharedObject.data["goldStars"];
 this.statusTextField.text="Star Count: "+starCount;
}