Kongregate API Integration for Flash IFrame GamesIn order to use the Kongregate Client API in your Flash game when it is hosted in an iframe, you will need to pass some Kongregate-specific Flash variables into your SWF. Preparing your HTMLIn order to pass in the appropriate Flash Variables, your page source will need to be modified slightly. The first step is including some Kongregate Javascript: <script src='http://www.kongregate.com/javascripts/kongregate_api.js'></script> Once this script is loaded, one can access a string of Kongregate Flash Variables with the following command: kongregateAPI.flashVarsString() This function will return a URL-encoded string, which will end in "&", so you can simply add any custom Flash Variables you need onto the end of it. You must then pass these Flash Variables into your SWF. For more information on how to do this, see this link
If you are using something like SWFObject to embed your SWF, it might be more convenient to have access to an kongregateAPI.flashVarsObject() Basic HTML ExampleHere is a very basic example of how to utilize the above information to pass the Kongregate Flash Variables into your SWF. Please note that this example is very crude, and it is more common to use something such as SWFObject or Adobe's tools to embed a SWF. <html> <body> <script type="text/javascript" src="http://www.kongregate.com/javascripts/kongregate_api.js"></script> <script language="javascript" type="text/javascript"> var flashvars = kongregateAPI.flashVarsString(); var html='<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"'; html += 'codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0"'; html += 'WIDTH="550" HEIGHT="400" id="game"><PARAM NAME="movie" ID="movie" VALUE="http://www.mysite.com/MyGame.swf">'; html += '<PARAM NAME="flashvars" VALUE="' + flashvars + '">'; // Add in flashvars html += '<EMBED src="http://www.mysite.com/MyGame.swf" WIDTH="550" HEIGHT="400"'; html += 'NAME="game" ID="game" TYPE="application/x-shockwave-flash"'; html += 'PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer"'; html += 'FLASHVARS="' + flashvars + '"></EMBED></OBJECT>'; // Add in flashvars document.write(html); </script> </body> </html> Preparing your SWF
You will need to make a call to It is typically most convenient to do this in the first SWF that is loaded for your game, and then pass a reference to the API object into any other SWF that will need it. Note that you will need to do this allowDomain call in any SWF that references the Kongregate API as well. This call needs to be done before you load the API SWF. This will allow communication between your parent SWF and our API. For more information about Security.allowDomain, see this link Here is a simple example of loading the AS3 API that sets permissions properly: import flash.display.LoaderInfo; import flash.display.Loader; import flash.net.URLRequest; import flash.events.Event; import flash.system.Security; var kongregate:*; // Function that loads the Kongregate API function initializeKongregateAPI():void{ // Pull the API path from the FlashVars var paramObj:Object = LoaderInfo(root.loaderInfo).parameters; var apiPath:String = paramObj.kongregate_api_path || "http://www.kongregate.com/flash/API_AS3_Local.swf"; Security.allowDomain(apiPath); // Load the API var request:URLRequest = new URLRequest(apiPath); var loader:Loader = new Loader(); loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onKongregateAPILoaded); loader.load(request); this.addChild(loader); } // Called when the Kongregate API is loaded function onKongregateAPILoaded( event:Event ):void{ kongregate = event.target.content; kongregate.services.connect(); } // Start the process of loading the API: initializeKongregateAPI();
If you pass the Security.allowDomain(kongregate.loaderInfo.url);
If you still can't get things to work, or find you are getting security exceptions, it also might be worth trying You're all set!You should now be able to utilize the Kongregate API just as if your game was hosted on Kongregate. |