Skip to content

Instantly share code, notes, and snippets.

@miniplay
Last active February 3, 2017 11:04
Show Gist options
  • Save miniplay/5012680 to your computer and use it in GitHub Desktop.
Save miniplay/5012680 to your computer and use it in GitHub Desktop.
Miniplay API - Loading of the AS3 API into a flash game (BASE)
/* 1. Imports */
import flash.display.LoaderInfo;
import flash.display.Loader;
import flash.net.URLRequest;
import flash.events.Event;
import flash.system.Security;
/* 2. Get the url from the flashvars */
var params:Object = LoaderInfo(root.loaderInfo).parameters; /* Access to the root object is required */
/* 3. If not present, load the latest available */
var apiUrl:String = params.mp_api_as3_url || "http://api.minijuegos.com/lechuck/static/as3/latest.swf";
/* 4. Grant the API access to this SWF */
Security.allowDomain(apiUrl); /* Having issues? try allowDomain("*")! */
/* 5. Load the API */
var apiRequest:URLRequest = new URLRequest(apiUrl);
var apiLoader:Loader = new Loader();
apiLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onApiLoadComplete); /* onApiLoad will be called once completed */
apiLoader.load(apiRequest);
this.addChild(apiLoader);
/* 6. API Reference (assigned inside onApiLoadComplete) */
var lechuck:*;
/* 7. On API Load Complete Handler */
function onApiLoadComplete(event:Event):void {
trace("API Loaded");
/* Assign the reference to the API instance */
lechuck = event.target.content;
/* Set api_token: Devel and production tokens, use devel if the mp_game_devel = 1 flashvar was received */
var token:String = lechuck.isDevel ? "YOUR_DEVEL_TOKEN" : "YOUR_PRODUCTION_TOKEN";
/* Connect the API backend */
trace("Connecting with the API backend...");
lechuck.connect(token, function():void {
if (lechuck.isConnected) {
trace("API connected, LeChuck services ready to rock!.");
if (lechuck.user.isGuest()) {
/* Boot the game for a guest user (ask to login) >>>> */
} else {
/* Boot the game for an authenticated user >>>> */
}
} else {
trace("API not connected: "+lechuck.connectError.message );
/* No API connection, boot the game? (you decide it) */
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment