{"_id":"56abcb4a678b58170031ec27","__v":15,"category":{"_id":"56abca6bf9757e0d007c6650","pages":["56abcb4a678b58170031ec27","56accf7513ac890d001c3c76","56acd12a693f0a0d0085f099","56acd43d0ab3c00d00ce332d","56acd698d21f1b0d00fd2f65"],"project":"56abbf55f25f160d00e17f4e","__v":5,"version":"56abbf55f25f160d00e17f51","sync":{"url":"","isSync":false},"reference":false,"createdAt":"2016-01-29T20:24:11.577Z","from_sync":false,"order":1,"slug":"api-overview","title":"API Languages"},"project":"56abbf55f25f160d00e17f4e","version":{"_id":"56abbf55f25f160d00e17f51","project":"56abbf55f25f160d00e17f4e","__v":12,"createdAt":"2016-01-29T19:36:53.665Z","releaseDate":"2016-01-29T19:36:53.665Z","categories":["56abbf56f25f160d00e17f52","56abca6bf9757e0d007c6650","56acddfa0ab3c00d00ce3332","56af65da9d32e30d0006d30f","56af66cab34d210d003d9ad0","56af6afcd21e9c0d00b628d1","56af6d6ecc4cbd0d00ce2c88","5705b12221cfed0e00e8c580","570a5676ade45d0e00c1ad33","570d7d25d1e4b82000d9e385","570eac3c3160d10e0041df0e","575709000fd6a3200010dded"],"is_deprecated":false,"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"","version_clean":"1.0.0","version":"1.0"},"parentDoc":null,"user":"56abbec30b9e0b0d00616274","updates":["56abcee335138c0d005c94c6"],"next":{"pages":[],"description":""},"createdAt":"2016-01-29T20:27:54.232Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","examples":{"codes":[]},"auth":"required","params":[],"url":""},"isReference":false,"order":1,"body":"#### Kongregate's ActionScript3 API\nThe Kongregate ActionScript 3 (AS3) API allows you to extend your game to communicate with the Kongregate back-end.\n\n### Dynamically loading the Kongregate AS3 API\n\nTo dynamically load the AS3 API, you must retrieve the API path from the [FlashVars](http://blogs.adobe.com/pdehaan/2006/07/using_flashvars_with_actionscr.html) and use a [Loader](http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/Loader.html) object to load the SWF – as you would any other. Once you load it in, you must keep a reference to the API object through whatever means you like.\n\nWhen testing locally, you will not have access to the live API. Instead, you can optionally have “shadow” services load that can assist you in validating proper interfacing with the API.\n\n### Example code\n\nBelow is some simple code that shows how to retrieve the API SWF path from the FlashVars and then load the API from Kongregate. If you are testing locally, and the FlashVars do not exist, then the \"shadow\" services will be loaded in instead.\n\nNote that to access FlashVars in AS3, you must have access to the root object, either from a [DisplayObject](http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/DisplayObject.html) already in the display list, or a reference saved from an active [DisplayObject](http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/DisplayObject.html). The means in which you handle this is your choice.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"import flash.display.LoaderInfo;\\nimport flash.display.Loader;\\nimport flash.net.URLRequest;\\nimport flash.events.Event;\\nimport flash.system.Security;\\n\\n// Pull the API path from the FlashVars\\nvar paramObj:Object = LoaderInfo(root.loaderInfo).parameters;\\n\\n// The API path. The \\\"shadow\\\" API will load if testing locally. \\nvar apiPath:String = paramObj.kongregate_api_path || \\n  \\\"http://cdn1.kongregate.com/flash/API_AS3_Local.swf\\\";\\n\\n// Allow the API access to this SWF\\nSecurity.allowDomain(apiPath);\\nSecurity.allowInsecureDomain(apiPath);\\n\\n// Load the API\\nvar request:URLRequest = new URLRequest(apiPath);\\nvar loader:Loader = new Loader();\\nloader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadComplete);\\nloader.load(request);\\nthis.addChild(loader);\\n\\n// Kongregate API reference\\nvar kongregate:*;\\n\\n// This function is called when loading is complete\\nfunction loadComplete(event:Event):void\\n{\\n    // Save Kongregate API reference\\n    kongregate = event.target.content;\\n\\n    // Connect to the back-end\\n    kongregate.services.connect();\\n\\n    // You can now access the API via:\\n    // kongregate.services\\n    // kongregate.user\\n    // kongregate.scores\\n    // kongregate.stats\\n    // etc...\\n}\",\n      \"language\": \"javascript\",\n      \"name\": \"ActionScript 3\"\n    }\n  ]\n}\n[/block]\n### The Kongregate API Object\n\nAs you can see in the function above, we keep a reference to the \"Kongregate API\" object and store it in the variable named `kongregate`. This is the assumption that is made for the remainder of the documentation, so if you store this reference in a different way, make sure to modify the examples accordingly.\n\n### Security/Cross-domain Considerations\n\nIf you need to access the API from a SWF that is hosted on another domain, then special measures must be taken. In order for our API to be able to perform callbacks in your SWF, you must first make a call to [Security.allowDomain](http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/system/Security.html#methodDetail) to grant permissions.\n\nOnce the SWF which intends to use the API has a reference to it (possibly by having it passed in from a preloader hosted on Kongregate), it should make the allowDomain call on the URL for the API. This can be done programmatically as shown in the example below:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"Security.allowDomain(kongregate.loaderInfo.url);\",\n      \"language\": \"javascript\",\n      \"name\": \"ActionScript 3\"\n    }\n  ]\n}\n[/block]\n### Using the AS3 API from an IFrame\n\nIf you need to access the AS3 API from a game hosted in an IFrame, you will need to pass along all the appropriate variables into your SWF. Documentation on how to do this can be found [here](doc:concepts-flash-iframe-integration).\n\nOnce the API is loaded, you can start using functions from the [Client API](doc:client-api-introduction) in your application.","excerpt":"","slug":"actionscript-3-api","type":"basic","title":"ActionScript 3 API"}

ActionScript 3 API


#### Kongregate's ActionScript3 API The Kongregate ActionScript 3 (AS3) API allows you to extend your game to communicate with the Kongregate back-end. ### Dynamically loading the Kongregate AS3 API To dynamically load the AS3 API, you must retrieve the API path from the [FlashVars](http://blogs.adobe.com/pdehaan/2006/07/using_flashvars_with_actionscr.html) and use a [Loader](http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/Loader.html) object to load the SWF – as you would any other. Once you load it in, you must keep a reference to the API object through whatever means you like. When testing locally, you will not have access to the live API. Instead, you can optionally have “shadow” services load that can assist you in validating proper interfacing with the API. ### Example code Below is some simple code that shows how to retrieve the API SWF path from the FlashVars and then load the API from Kongregate. If you are testing locally, and the FlashVars do not exist, then the "shadow" services will be loaded in instead. Note that to access FlashVars in AS3, you must have access to the root object, either from a [DisplayObject](http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/DisplayObject.html) already in the display list, or a reference saved from an active [DisplayObject](http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/DisplayObject.html). The means in which you handle this is your choice. [block:code] { "codes": [ { "code": "import flash.display.LoaderInfo;\nimport flash.display.Loader;\nimport flash.net.URLRequest;\nimport flash.events.Event;\nimport flash.system.Security;\n\n// Pull the API path from the FlashVars\nvar paramObj:Object = LoaderInfo(root.loaderInfo).parameters;\n\n// The API path. The \"shadow\" API will load if testing locally. \nvar apiPath:String = paramObj.kongregate_api_path || \n \"http://cdn1.kongregate.com/flash/API_AS3_Local.swf\";\n\n// Allow the API access to this SWF\nSecurity.allowDomain(apiPath);\nSecurity.allowInsecureDomain(apiPath);\n\n// Load the API\nvar request:URLRequest = new URLRequest(apiPath);\nvar loader:Loader = new Loader();\nloader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadComplete);\nloader.load(request);\nthis.addChild(loader);\n\n// Kongregate API reference\nvar kongregate:*;\n\n// This function is called when loading is complete\nfunction loadComplete(event:Event):void\n{\n // Save Kongregate API reference\n kongregate = event.target.content;\n\n // Connect to the back-end\n kongregate.services.connect();\n\n // You can now access the API via:\n // kongregate.services\n // kongregate.user\n // kongregate.scores\n // kongregate.stats\n // etc...\n}", "language": "javascript", "name": "ActionScript 3" } ] } [/block] ### The Kongregate API Object As you can see in the function above, we keep a reference to the "Kongregate API" object and store it in the variable named `kongregate`. This is the assumption that is made for the remainder of the documentation, so if you store this reference in a different way, make sure to modify the examples accordingly. ### Security/Cross-domain Considerations If you need to access the API from a SWF that is hosted on another domain, then special measures must be taken. In order for our API to be able to perform callbacks in your SWF, you must first make a call to [Security.allowDomain](http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/system/Security.html#methodDetail) to grant permissions. Once the SWF which intends to use the API has a reference to it (possibly by having it passed in from a preloader hosted on Kongregate), it should make the allowDomain call on the URL for the API. This can be done programmatically as shown in the example below: [block:code] { "codes": [ { "code": "Security.allowDomain(kongregate.loaderInfo.url);", "language": "javascript", "name": "ActionScript 3" } ] } [/block] ### Using the AS3 API from an IFrame If you need to access the AS3 API from a game hosted in an IFrame, you will need to pass along all the appropriate variables into your SWF. Documentation on how to do this can be found [here](doc:concepts-flash-iframe-integration). Once the API is loaded, you can start using functions from the [Client API](doc:client-api-introduction) in your application.