AS3: Site selective API integration – Part II

And the next API integration post. The problem of the previous version was that the swf was not capable of playing as a standalone. This is now solved via flash.system.Capabilities.

The following code checks where the swf is played, in the browser (plugin for FF, activex for IE), the Flash IDE or as a standalone. If it is played in a browser it checks for the domain name and launches the appropriate API. I inserted three of them in this example, another one is always my homepage and a different logo screen is launched on FGL while in bidding mode to show sponsors what they bid for.

package
{
	import flash.system.Capabilities;
	import flash.display.Stage;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.text.TextField;
	import flash.net.navigateToURL;
	import flash.net.URLRequest;
	import mochi.as3.*
	import NewgroundsAPI;
	public dynamic class DocumentClass extends MovieClip
	{
		public var wrapper:Object;
		public var kText:String;
		public var pText:String;
		public var lText:String;
		public var domainCheck:String;
		public var mochConn:Boolean = true;
		public var kongConn:Boolean = false;
		public var newgConn:Boolean = false;
		public var mindConn:Boolean = false;
		public var kongregate:*;
		public var MindJoltAPI:*;

		public function DocumentClass ()
		{
			this.addEventListener(Event.ADDED_TO_STAGE, initGame, false, 0, true);
		}

		public function initGame (event:Event)
		{
			this.removeEventListener(Event.ADDED_TO_STAGE, initGame);
			trace("*** Game initiated ***");

			MochiServices.connect("XXXXXXXXXXXXXXXX", root, onMochiConnectError);

			wrapper = root.parent;
			while ( wrapper.parent ) {
			wrapper = wrapper.parent;
			}

			kText = "Standard";
			pText = Capabilities.playerType;
			lText = Capabilities.language;

			kText = "Browser";
			domainCheck = wrapper.loaderInfo.url;
			dTextDisplay.text = domainCheck;

			if ( pText == "External" || pText == "StandAlone" ) {
				kText = "Flash Player";
			}
			if ( pText == "PlugIn" || pText == "ActiveX" ) {
				kText = "Browser";
				domainCheck = wrapper.loaderInfo.url;
				dTextDisplay.text = domainCheck;

				if ( domainCheck.indexOf("kongregate") >= 0 ) {
					kText = "kongregate";
					kongConn = true;
					var paramObj:Object = wrapper.loaderInfo.parameters;
					var api_url:String = paramObj.api_path || "http://www.kongregate.com/flash/API_AS3_Local.swf";
					trace ( "API path: " + api_url );
					var request:URLRequest = new URLRequest ( api_url );
					var loader:Loader = new Loader();
					loader.contentLoaderInfo.addEventListener ( Event.COMPLETE, loadComplete );
					loader.load ( request );
					wrapper.addChild ( loader );
					function loadComplete ( event:Event ):void {
					kongregate = event.target.content;
					kongregate.services.connect();
					}
				}
				if (domainCheck.indexOf("newgrounds") >= 0 || domainCheck.indexOf("ungrounded") >= 0 ) {
					kText = "newgrounds";
					newgConn = true;
					NewgroundsAPI.linkAPI(this);
					NewgroundsAPI.connectMovie(XXXX);
				}
				if (domainCheck.indexOf("mindjolt") >=0 ) {
					kText = "mindjolt";
					mindConn = true;
					var gameParams:Object = wrapper.loaderInfo.parameters;
					var urlLoader:Loader = new Loader();
					urlLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadFinished);
					urlLoader.load(new URLRequest(gameParams.mjPath || "http://static.mindjolt.com/api/as3/scoreapi_as3_local.swf"));
					wrapper.addChild(urlLoader);
					function loadFinished (e:Event):void {
				    	MindJoltAPI=e.currentTarget.content;
				    	MindJoltAPI.service.connect();
					}
				}
			}
		}
	//doing game stuff from here on...

I used kText, lText and pText in dynamic textfields in the swf to check if the compiled swf with applied version control still got the correct parameters. mindConn, kongConn and so on are used for different highscore submissions on the specific sites.

Yoho!

This entry was posted in game development, mochiads and tagged , , , , . Bookmark the permalink.

Comments are closed.