HTML5开发-在你的游戏应用中加入广告

在中国,一般都是不喜欢花钱的,如果你的游戏或者应用是需要付费的,那么玩的人就少。所以大多数开发者都会选择在游戏中植入广告,来获取些许微薄的收入。

下面我来说说如何利用lufylegend.js开源引擎在游戏中加入广告。

利用lufylegend.js引擎1.7.2版中的新功能LStageWebView,可以在html中加入一个iframe用来显示一个外部页面,我们可以利用这个功能来显示广告。

lufylegend.js引擎官网

http://lufylegend.com/lufylegend


我使用了google adsense广告,当然你也可以使用其他广告,方法一样,好了现在开始。

第一步,你要有一个网站。

第二步,要在google adsense申请注册一个广告。

注:会根据你网站的内容进行审核,申请不通过的话,可以好好看一下google adsense的协议

第三步,在网站上建一个html文件,加入广告代码。

[html]  view plain copy
  1. <html>  
  2. <head>  
  3. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
  4. </head>  
  5. <body>  
  6. <script type="text/javascript"><!--  
  7. google_ad_client = "ca-pub-20477800********";  
  8. google_ad_slot = "2255******";  
  9. google_ad_width = 468;  
  10. google_ad_height = 60;  
  11. //-->  
  12. </script>  
  13. <script type="text/javascript"  
  14. src="http://pagead2.googlesyndication.com/pagead/show_ads.js">  
  15. </script>  
  16. </body>  
  17. </html>  

上面的js代码,在google adsense的管理页面可直接获取,很简单。

第四步,在游戏中加入以下代码。

[javascript]  view plain copy
  1. var webView = new LStageWebView();  
  2. webView.setViewPort(new LRectangle(10,680,LGlobal.width,60));  
  3. webView.loadURL("http://lufylegend.com/ad/game_vertical_01.html");  
  4. webView.show();  
loadURL函数是所要显示的外部网页地址,我这个地址是我在第三步所建的html。


完成了,测试连接:

http://lufylegend.com/demo/GemGemAd/

运行效果如下:

HTML5开发-在你的游戏应用中加入广告_第1张图片

LStageWebView使用起来很简单,更详细的使用,可以参照一下官方的API文档

你可能感兴趣的:(HTML5开发-在你的游戏应用中加入广告)