MindJolt Score Submission API for AS3 and AS2
2009/10/05 - Deprecated old AS2 api that used LocalConnection. Due to recent bugs in LocalConnection (no longer works reliably on Macintosh computers), we have decided to require all new games to use our updated AS2 api listed below. This now also keeps our AS2 and AS3 api's very consistent.
2009/09/04 - New versions of AS3 and AS2 apis to support our new virtual goods system (still in closed beta), and to stop using LocalConnection. The new AS2 api no longer uses LocalConnection. Instead, it now works like our AS3 version.
2008/02/27 - The MindJolt AS3 api is currently in beta release.
Installation using actionscript code snippet
This code snippet needs to happen as EARLY as possible when your game starts.
# Load the API
You will need access to "root" to be able to get the path to the API that we'll pass in via FlashVars. After you load the API from that path, you need to keep track of the API instance yourself, so you have it when you want to make API calls.
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.net.URLRequest;
import flash.events.Event;
// You'll use this variable to access the API
// (make sure you can access it from wherever you will later call submitScore)
var MindJoltAPI:Object;
//////
// All of this code should be executed at the very beginning of the game
//
// this function is called after everything has been successfully loaded
// it is good to wait for this method to be called before attempting to use the API
function postMindJoltAPIConnect (success:Boolean) {
trace("[MindJoltAPI] service successfully loaded");
}
if (MindJoltAPI == null) {
// get the parameters passed into the game
Security.allowDomain("static.mindjolt.com");
// get the parameters passed into the game
var gameParams:Object = LoaderInfo(root.loaderInfo).parameters;
// now load the API
var urlLoader:Loader = new Loader();
urlLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadFinished);
urlLoader.load(new URLRequest(gameParams.mjPath || "http://static.mindjolt.com/api/as3/api_as3_local.swf"));
this.addChild(urlLoader);
function loadFinished (e:Event):void {
MindJoltAPI=e.currentTarget.content;
if (MindJoltAPI != null) {
MindJoltAPI.service.connect(postMindJoltAPIConnect);
trace ("[MindJoltAPI] service successfully loaded");
} else {
trace("[MindJoltAPI] failed to load");
}
}
}
# Load the API
You will need access to "_root" to be able to get the path to the API that we'll pass in via FlashVars. After you load the API from that path, you need to keep track of the API instance yourself, so you have it when you want to make API calls. We create an empty movie clip, and load our API SWF into it.
// You'll use this variable to access the API
// (make sure you can access it from wherever you will later call submitScore)
var MindJoltAPI:MovieClip;
//////
// All of this code should be executed at the very beginning of the game
//
// this function is called after everything has been successfully loaded
// it is good to wait for this method to be called before attempting to use the API
function postMindJoltAPIConnect (success:Boolean) {
trace("[MindJoltAPI] service successfully loaded");
}
if (MindJoltAPI == undefined) {
System.security.allowDomain("static.mindjolt.com");
MindJoltAPI = createEmptyMovieClip("MindJoltAPI", getNextHighestDepth());
var apiPath:String = _level0["mjPath"] != undefined ? _level0["mjPath"] : "http://static.mindjolt.com/api/as2/api_as2_local.swf";
var apiLoader:MovieClipLoader = new MovieClipLoader();
// create some listener functions to be called after our API is loaded
var apiLoadListener = new Object();
apiLoader.addListener(apiLoadListener);
apiLoadListener.onLoadInit = function() { MindJoltAPI.service.connect(postMindJoltAPIConnect); }
apiLoadListener.onLoadError = function() { trace("[MindJoltAPI] failed to load."); }
// now load the API
apiLoader.loadClip(apiPath, MindJoltAPI);
}
Note: The API is being loaded into a new empty movieclip with a pre-defined depth of 1000000. We chose this depth, instead of just using getNextHighestDepth(), because so many game developers seem to manually manage their depths. You can change this depth to another depth if you need to.
How to use it!!!
# Submitting Score
You simply need to provide the score. And, if you use game modes, you should provide the current mode the player is playing in. (Example game modes: "easy", "normal", "hard", etc.)
The submitScore call should be placed wherever you compute the final score for the game.
For example, suppose you store the score in a variable called "currentScore".
- Submitting a score without a game mode
MindJoltAPI.service.submitScore(currentScore);
- Submitting a score with a game mode
MindJoltAPI.service.submitScore(currentScore, "easy");
Only put in a game mode if you have more than one mode in your game. This allows us to track the high scores for each game mode separately.
Note: You will need to have access to MindJoltAPI from wherever you loaded it up above. For example, in AS2, if you loaded the api on the timeline, then you might access the api as _root.MindJoltAPI.service.submitScore(currentScore);
|