|
Kongregate的Javascript APIKongregate Javascript API允许您扩展您的游戏并与Kongregate后端沟通。包括我们的JavaScript API,您可以访问我们ActionScript API提供的大多数功能。
加载API为了加载Kongregate API对象,您需要包含一个脚本标记,用它加载我们的JavaScript源文件。该脚本标记应放置在您的文档的头部标记。 <script src='http://www.kongregate.com/javascripts/kongregate_api.js'></script>
Kongregate API对象JavaScript API会自动创建一个全局的变量名为kongregateAPI。您可以使用此对象通过loadAPI()和getAPI()函数来加载Kongregate API服务: loadAPI(回调)
例如: 加载API,加载API,并调用onComplete函数当加载完成时。 function when loading is done.
// 加载API
var kongregate;
kongregateAPI.loadAPI(onComplete);
// 回调函数
function onComplete(){
// 制定全球kongregate API对象
kongregate = kongregateAPI.getAPI();
}
正如你所看到的,一旦回调函数被调用,您可以使用getAPI函数创建一个到Kongregate API对象的引用。这将允许您访问的其他文件所描述的各种服务。重要的是每个Kongregate游戏页面加载只能使这个调用一次。如果您重新加载文件且该文件包含此调用,或从多个页面调用,情况将打破。为了解决这一问题,可考虑使用如下所述的游戏外壳。
使用Kongregate外壳植入您的游戏为了您的方便,Kongregate提供了一个简单的HTML外壳(右击另保存),你可以自定义将正确加载并初始化的JavaScript API。该外壳采用了kongregateAPI对象的embedFrame方法正确将您的游戏植入到附属frame中。之后您的游戏可以正常运作,同时还总能通过parent.访问Kongregate API。为了使用外壳,你必须简单地将外壳存放在您的域中,并修改embedFrame调用以指向您服务器上的正确位置。 embedFrame(path,elementID)
例如:把一个游戏iframe植入到一个名为“contentdiv”的元素中。
// 通过认证传送user_ID的和game_auth_token参数。
var params = "kongregate_user_id=" + kongregate.services.getUserId() +
"&kongregate_game_auth_token=" + kongregate.services.getGameAuthToken();
// 将kongregate-game.php文件植入到名为"contentdiv"的元素中"
kongregateAPI.embedFrame("kongregate-game.php?" + params, "contentdiv");
请注意,外壳将无法正常工作除非它被植入到到一个Kongregate游戏网页。为了测试它,你应该修改你的游戏设置,并将Iframe的URL设置成在您服务器上的shell文件的位置。 举例外壳文件的内容可以在下面找到。正如你所看到的,它只是设置了一个占位符DIV元素,并调用embedFrame函数一旦API已经正确初始化。这确保了API只加载了一次,并且您在您的IFRAME做的任何东西都被正确地保存,以保证连接没有断开。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Kongregate Game Shell</title>
<!-- Load the Kongregate Javascript API -->
<script type="text/javascript" src="http://www.kongregate.com/javascripts/kongregate_api.js"></script>
<!-- Give the shell no border/scroll bars and match the Kongregate background color.
If your game needs scrollbars, you might need to modify these styles -->
<style type="text/css">
html{border: none; overflow: hidden; background-color: #333; height: 100%;}
body{border: none; background-color: #333;margin:0; padding:0;}
</style>
</head>
<body>
<script type="text/javascript">
// 当API完成加载后回调
function onLoadCompleted(){
// Get a global reference to the kongregate API. This way, pages included in the
// iframe can access it by using "parent.kongregate"
kongregate = kongregateAPI.getAPI();
// Embed the game into the "contentdiv" div, which is defined below. You can also
// manually create your own iframe, this function is just for convenience.
// This example also passes along the kongregate user_id and game_auth_token so
// that the page can use them for authentication.
var params = "kongregate_user_id=" + kongregate.services.getUserId() +
"&kongregate_game_auth_token=" + kongregate.services.getGameAuthToken();
kongregateAPI.embedFrame("game.html?" + params);
}
// Begin the process of loading the Kongregate API:
kongregateAPI.loadAPI(onLoadCompleted);
</script>
<!-- The div that the game will be placed into. Make sure to set the width and height properly -->
<div id="contentdiv" style="top:0px; left:0px; width:700px; height:500px; borders:none;">
<!-- You can manually put your game iframe in here instead of calling embedFrame above if you wish -->
</div>
</body>
</html>
例子你可以找到一个执行的例子演示如何使用Shell加载API,以及如何从JavaScript这里调用API函数
API文档: |