Last active
February 3, 2017 11:04
-
-
Save miniplay/5012909 to your computer and use it in GitHub Desktop.
Miniplay API - Loading of the AS3 API into a flash game (EXTENDED). Additional content provided, refer to the BASE for the simplest version.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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"; | |
// apiUrl+="?log_level=1"; /* Custom loglevel can be provided as a parameter (By default, it uses the mp_log_level flashvar) */ | |
/* 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; | |
/* Custom logLevel can be provided manually */ | |
lechuck.logLevel(2); /* 1 - Spam, 2 - Debug, 3 - Info, 4 - Warn, 5 -Error */ | |
/* Attach API logtrace listener, the API will trace instead messages of logging them to the browser console if it's not available */ | |
/* If it's available, this event won't be called, and the log will be sent directly to the browser console */ | |
/* This is totally unrequired, it's only provided as an extra feature*/ | |
lechuck.onTrace(function(evt:*):void { | |
trace("Log: "+ evt.msg); /* Handle it however you want */ | |
}); | |
/* Attach custom event listeners */ | |
/* Allows us to attach our own listeners to various API events */ | |
lechuck.onUserDisconnect(function():void { | |
lechuck.logInfo("Custom Listener: User disconnected"); | |
}); | |
lechuck.onConnect(function():void { | |
lechuck.logInfo("Custom Listener: Connected"); | |
}); | |
lechuck.onUnableToConnect(function():void { | |
lechuck.logError("Custom Listener: Unable to connect"); | |
}); | |
lechuck.onDisconnect(function():void { | |
lechuck.logInfo("Custom Listener: Disconnected"); | |
}); | |
lechuck.onResize(function():void { | |
lechuck.logInfo("Custom Listener: Game resized!"); | |
}); | |
/* 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"; | |
/* We can now use logInfo instead of trace, it will log them to browser console (depending on loglevel), or */ | |
/* trace them if the browser console is not available (you can capture the traces and handle them differently) */ | |
lechuck.logInfo("Connecting with the API backend..."); | |
/* Connect the API backend */ | |
lechuck.connect( token, function():void { | |
if (lechuck.isConnected) { | |
lechuck.logInfo("API connected, LeChuck services ready to rock!."); | |
/* API modules that can be called now: */ | |
// lechuck.user | |
// lechuck.stat | |
// lechuck.item | |
// lechuck.datastore | |
// lechuck.sharedcontent | |
// lechuck.environment | |
/* User status and game booting */ | |
if (lechuck.user.isGuest()) { | |
/* Boot the game for a guest user (ask to login) >>>> */ | |
/* If your game has already booted show main menu for guests */ | |
/* Remember: Miniplay registration OR login will issue a page refresh! */ | |
/* any progress made by guests users cannot be saved */ | |
} else { | |
lechuck.logInfo("User logged in: " + lechuck.user.uid); | |
/* Boot the game for an authenticated user >>>> */ | |
/* If your game has already booted show main menu */ | |
} | |
/* An onConnect event listener was attached and will be dispatched automatically after this*/ | |
} else { | |
lechuck.logError("API not connected: "+lechuck.connectError.message ); | |
/* No API connection, boot the game? (you decide it) */ | |
/* An onUnableToConnect event listener was attached and will be dispatched automatically after this */ | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment