Examples & Tutorials
Adding the Initial Andrograde API Code
In your main class when starting your game:
//Note: you can import Andrograde.*; so you don't need to prepend Andrograde. on every call.
var AndrogradeAPI:Andrograde.APIClient;
AndrogradeAPI = new Andrograde.APIClient("your api code", APILoaded, true); // the 3rd parameter turns on debugging
addChild(AndrogradeAPI);
//Note: if you are using Flex you may have to do rawChildren.addChild instead
function APILoaded(success:Boolean):void {
if (success) {
// API initialized successfully, set up your Andrograde API dependant stuff here
} else {
// API failed to initialize, usually this is because you are disconnected or using a bad API key
}
}
Logging in a Player
The Andrograde API does it's best to prompt users to login whenever necessary but you can also choose to display the login prompt at your convenience.
// pause your game here
AndrogradeAPI.login(UserLoggedIn); // display the login prompt
function UserLoggedIn(success:Boolean):void {
// unpause your game here
if (success) {
// login/register succeeded
} else {
// login/register failed or was cancelled
}
}
You can even use the customizable Andrograde UI features to display a login button to click to bring up the login prompt
import Andrograde.APIButton;
var login_button:APIButton;
function UserLoggedIn(success:Boolean):void {
// unpause your game here
if (success) { // login/register succeeded
removeChild(login_button);
} else {
login_button.enabled= true;
}
}
function loginClick(e:Event):void {
// pause your game here
login_button.enabled= false;
AndrogradeAPI.login(UserLoggedIn); // display the login prompt
}
login_button = new APIButton(0, 400, AndrogradeAPI.UI.text("LOGIN",AndrogradeAPI.UI.COLOR_BUTTON_TEXT), loginClick);
addChild(login_button);
You can check if a user is logged in or not with the following code
if (AndrogradeAPI.user) {
// user is logged in
} else {
// user is not logged in, you can display the login panel here
}
Selling items and features in your game
Note: If the user is not already logged in, a login panel will pop up before the buy panel
function buy_complete(success:Boolean):void {
// unpause your game here
if (success) {
// the purchase was successful
} else {
// the purchase failed
}
}
// pause your game here
AndrogradeAPI.buy(50, "my item", buy_complete); // my item costs 50 points to buy
To check if a player already owns something just do
if (AndrogradeAPI.user && AndrogradeAPI.user.owns("my item")) {
// add whatever 'my item' is in the game here
} else {
// player does not own 'my item', don't add it to the game here
}
When a user buys an item in your game, your game will be credited those points. You can also check how many points a user has:
trace("user has " + AndrogradeAPI.user.points + " points");
Offering a Paid Full Version for your game
Note: If the user is not already logged in, a login panel will pop up before the upgrade panel
if (!AndrogradeAPI.user || !AndrogradeAPI.user.owns("full version")) {
// pause your game here
function buy_complete(success:Boolean):void {
// unpause your game here
if (success) {
// they bought the full version
} else {
// they didn't buy it
}
}
AndrogradeAPI.buy(500, "full version", buy_complete);
}
Setting and Getting Stats
Getting player's rank for a stat
Getting stats for another player
function getinfo(user:User):void {
trace("Will's highscore: " + user.getStat('high score'));
trace("Does Will own the item? " + user.owns('my item'));
// you can do many more things with this user...
});
// You must pass in the user's identity (not their name) here, you can get this from User.identity
AndrogradeAPI.getUserInfo("Will.0", getinfo);
Setting stats for another player
function getinfo(user:User):void {
user.setStat("highscore", 300, User.STAT_REPLACE, function (success:Boolean):void {
trace("Will's updated HS " + user.getStat('highscore'));
});
});
AndrogradeAPI.getUserInfo("Will.0", getinfo);
Getting High Scores
You can grab the top or bottom most 'values' of any stat you are keeping track of and display them
however you want
function highscoreCallback(scores:Array):void {
if (scores==null) return;
for (var i:uint = 0; i < scores.length; i++) {
if (scores[i][1] != null) {// if they actually got a highscore for this
//Display the highscores here however you like
//You can use the identity to call getUserinfo and get more info about that user
trace("identity: " + scores[i][0] + " name: " + scores[i][1] + " score: " + scores[i][2]);
}
}
}
// Get the top 10 highscores of the stat named 'highscore' starting at offset 0
AndrogradeAPI.getHighScores("highscore", APIClient.HIGHSCORE_HIGHEST, 0, 10, highscoreCallback);
Of course you can then display the highscores however you want but Andrograde comes with a
fully customizable UI can can use as well. Lets see how we would combine this with the Andrograde UI components to build a fast highscore display
const NUM_SCORES_TO_FETCH:uint = 10;
var startingOffset:uint = 0;
var highscoresNames:TextField = new TextField();
var highscoresScores:TextField = new TextField();
var highscoresPanel:Sprite = new Sprite();
function prevClick(e:Event):void {
if (startingOffset >= NUM_SCORES_TO_FETCH) {
startingOffset -= NUM_SCORES_TO_FETCH;
prev.alpha = 0.5;
prev.mouseEnabled = false; // don't allow them to click the button twice before it fetching the stuff
AndrogradeAPI.getHighScores("highscore", APIClient.HIGHSCORE_HIGHEST, startingOffset, NUM_SCORES_TO_FETCH, highscoreCallback);
}
}
// make the 'prev' button, if clicked will show more scores
var prev:APIButton = new APIButton(0, 160, AndrogradeAPI.UI.text("Prev",AndrogradeAPI.UI.COLOR_BUTTON_TEXT), prevClick);
highscoresPanel.addChild(prev);
function nexClick(e:Event):void {
startingOffset += NUM_SCORES_TO_FETCH;
next.alpha = 0.5;
next.mouseEnabled = false; // don't allow them to click the button twice before it fetching the stuff
AndrogradeAPI.getHighScores("highscore", APIClient.HIGHSCORE_HIGHEST, startingOffset, NUM_SCORES_TO_FETCH, highscoreCallback);
}
// make the 'next' button, if clicked will show more scores
var next:APIButton = new APIButton(150, 160, AndrogradeAPI.UI.text("Next",AndrogradeAPI.UI.COLOR_BUTTON_TEXT), nexClick);
highscoresPanel.addChild(next);
function highscoreCallback(scores:Array):void {
if (scores==null) return;
// reenable the previous and next buttons
next.alpha = 1;
next.mouseEnabled = true;
prev.alpha = 1;
prev.mouseEnabled = true;
if (highscoresPanel.contains(highscoresNames)) highscoresPanel.removeChild(highscoresNames);
if (highscoresPanel.contains(highscoresScores)) highscoresPanel.removeChild(highscoresScores);
highscoresNames.multiline = true;
highscoresScores.multiline = true;
highscoresNames.htmlText = "";
for (var i:int = 0; i < scores.length; i++) {
if (scores[i][1] != null) {// if they actually got a highscore for this
if (i + startingOffset == rank-1) // render yourself as bold
highscoresNames.htmlText += "<b>" + scores[i][1] + "</b><br>";
else // render everyone else normal
highscoresNames.htmlText += scores[i][1] + "<br>";
}
}
highscoresScores.htmlText = "";
for (i = 0; i < scores.length; i++) {
if (scores[i][1] != null) {// if they actually got a highscore for this
if (i + startingOffset == rank-1) // render yourself as bold
highscoresScores.htmlText += "<b>" + scores[i][2] + "</b><br>";
else
highscoresScores.htmlText += scores[i][2] + "<br>";
}
}
highscoresNames.width = 100;
highscoresNames.height = 158;
highscoresNames.x = 10;
highscoresNames.y = 0;
highscoresPanel.addChild(highscoresNames);
highscoresScores.width = 85;
highscoresScores.height = 158;
highscoresScores.x = 110;
highscoresScores.y = 0;
highscoresPanel.addChild(highscoresScores);
highscoresPanel.graphics.clear();
AndrogradeAPI.UI.panel(highscoresPanel, null, null, 1);
}
AndrogradeAPI.getHighScores("highscore", APIClient.HIGHSCORE_HIGHEST, startingOffset, NUM_SCORES_TO_FETCH, highscoreCallback);
Game Variables You Can Change After Release
You can use this to control variables inside your game based on player feedback long after you have deployed your game all over the net.
In the
Edit Game popup on the
Upload Page
There is a box for
API Params. Fill in your parameters and values in here, seperate variables with new lines:
myVariable = 42
myOtherVariable = Welcome to my game!
In your game code:
trace("myVariable = " + AndrogradeAPI.parameters_server.myVariable);
trace("myOtherVariable = " + AndrogradeAPI.parameters_server.myOtherVariable);
You can use these like any other variable in Actionscript, the possibilities are endless!
Selling items and features with a controllable price
// remember to pause your game before popping this dialog up!
function buy_complete(success:Boolean):void {
// unpause your game here
if (success) {
// the purchase was successful
} else {
// the purchase failed
}
}
// pause your game here
AndrogradeAPI.buy(AndrogradeAPI.parameters_server.myitem, "my item", buy_complete);
In the
Edit Game popup on the
Upload Page
There is a box for
API Params. Fill in your parameters and values in here, seperate variables with new lines:
myitem = 50
When a user buys an item in your game, your game will be credited those points. You can also check how many points a user has:
trace("user has " + AndrogradeAPI.user.points + " points");
Save and Share user generated content
Note: this only works on Andrograde.com with a logged in user
Parameters passed to an Andrograde game url are automatically added to the
API.parameters along with the
runtime changable server synced variables.
When you upload your game to Andrograde, click on the 'view game' link. For example, the API test game view game goes to
http://andrograde.com/games.php?game=Andrograde_API_TestGame
You can add parameters to the url like so
http://andrograde.com/games.php?game=Andrograde_API_TestGame?level=testuser
Then in the game you can get the url parameters like so
trace(AndrogradeAPI.parameters_server.level); // outputs testuser
Combine this with
stat loading and
stat saving to make sharable user generated content.
Step 1 is to save the level the player created when they are in the level editor:
var level:String; // the level data
level= "some user generated level content here";
function savedLevel(success:Boolean):void {
if (success) {
// level save successful!
}
});
AndrogradeAPI.user.setStat("level", level, Andrograde.User.STAT_REPLACE, savedLevel);
Step 2 is to load the level data for the level you are playing on based on the url parameter:
if (AndrogradeAPI.parameters.level) {
function getinfo(user:User):void {
level= user.getStat('level');
//You can now display that user's level
}
AndrogradeAPI.getUserInfo(AndrogradeAPI.parameters_server.level, getinfo);
}
Step 3 is to show the player a link for them to share their level with their friends
var link_to_display:String= "http://andrograde.com/games.php?game=Andrograde_API_TestGame?level=" + AndrogradeAPI.user.name;