1. var TAG_SPRITE_BALL = ;
  2. var TAG_SPRITE_USER = ;
  3. var TAG_SPRITE_NPC = ;
  4. var PTM_RATIO = ;
  5. var GRABABLE_MASK_BIT = <<;
  6. var NOT_GRABABLE_MASK = ~GRABABLE_MASK_BIT;
  7.  
  8. var MainLayer = cc.Layer.extend({
  9. _ball:null,
  10. _man:null,
  11. _rpc:null,
  12. _leftGoal:null,
  13. _rightGoal:null,
  14. _leftPower:null,
  15. _rightPower:null,
  16. _time:,
  17.  
  18. space:null,
  19.  
  20. ctor:function () {
  21. this._super();
  22. var size = cc.director.getWinSize();
  23.  
  24. this._ball = new Ball();
  25. this._ball.x = size.width / ;
  26. this._ball.y = size.height / ;
  27. this.addChild(this._ball);
  28.  
  29. // 左方向
  30. var btnLeft = cc.MenuItemImage.create(
  31. res.btn_left_up,
  32. res.btn_left_down,
  33. function () {
  34. this._man.runLeft();
  35. }, this);
  36. btnLeft.x = ;
  37.  
  38. // 右方向
  39. var btnRight = cc.MenuItemImage.create(
  40. res.btn_right_up,
  41. res.btn_right_down,
  42. function () {
  43. this._man.runRight();
  44. }, this);
  45. btnRight.x = ;
  46.  
  47. // power
  48. var btnPower = cc.MenuItemImage.create(
  49. res.btn_power_up,
  50. res.btn_power_down,
  51. function () {
  52. this._man.power();
  53. }, this);
  54. btnPower.x = ;
  55.  
  56. // jump
  57. var btnJump = cc.MenuItemImage.create(
  58. res.btn_jump_up,
  59. res.btn_jump_down,
  60. function () {
  61. this._man.jump();
  62. }, this);
  63. btnJump.x = ;
  64.  
  65. // kick
  66. var btnKick = cc.MenuItemImage.create(
  67. res.btn_kick_up,
  68. res.btn_kick_down,
  69. function () {
  70. this._man.kick();
  71. }, this);
  72. btnKick.x = ;
  73.  
  74. // 暂停
  75. var btnPause = cc.MenuItemImage.create(
  76. "res/pause.png",
  77. "res/pause2.png",
  78. function () {
  79. this.onStop();
  80. }, this);
  81. btnPause.x = size.width - ;
  82. btnPause.y = size.height - ;
  83.  
  84. var menu = cc.Menu.create(btnLeft, btnRight, btnPower, btnJump, btnKick, btnPause);
  85. menu.x = ;
  86. menu.y = ;
  87. this.addChild(menu, );
  88.  
  89. // 主角
  90. this._man = new Footballer_cn();
  91. this._man.flippedX = true;
  92. this._man.x = ;
  93. this._man.y = ;
  94. this._man.anchorX = 0.5;
  95. this._man.anchorY = ;
  96. this.addChild(this._man,,TAG_SPRITE_USER);
  97. this._npc = new Footballer_br();
  98. this._npc.x = size.width - ;
  99. this._npc.y = ;
  100. this._npc.anchorX = 0.5;
  101. this._npc.anchorY = ;
  102. this.addChild(this._npc,,TAG_SPRITE_NPC);
  103.  
  104. // 球门
  105. this._leftGoal = new Goalpost(true);
  106. this._leftGoal.x = ;
  107. this._leftGoal.y = ;
  108. this._leftGoal.anchorX = ;
  109. this._leftGoal.anchorY = ;
  110. this.addChild(this._leftGoal);
  111.  
  112. this._rightGoal = new Goalpost();
  113. this._rightGoal.x = size.width;
  114. this._rightGoal.y = ;
  115. this._rightGoal.anchorX = ;
  116. this._rightGoal.anchorY = ;
  117. this.addChild(this._rightGoal);
  118.  
  119. // power
  120. this._leftPower = new PowerProgress(size.width/-,size.height-,,0.5,this);
  121. this.addChild(this._leftPower, );
  122. this._rightPower = new PowerProgress(size.width/+,size.height-,,0.5,this);
  123. this.addChild(this._rightPower, );
  124.  
  125. // 系统计划任务,即每帧调用update函数
  126. this.scheduleUpdate();
  127. // 自定义计划任务
  128. this.schedule(this.uiSchedule, );
  129.  
  130. cc.sys.dumpRoot();
  131. cc.sys.garbageCollect();
  132.  
  133. this.initChipmunk();
  134.  
  135. return true;
  136. },
  137. onStop:function () {
  138. this._bStop = !this._bStop;
  139. if (this._bStop == true) {
  140. cc.director.pause();
  141. }
  142. else {
  143. cc.director.resume();
  144. }
  145. },
  146.  
  147. update:function (dt) {
  148. this.space.step(dt);
  149. },
  150. uiSchedule:function () {
  151. this._time++;
  152. this._leftPower.showPower();
  153. this._rightPower.showPower();
  154. },
  155. initChipmunk:function() {
  156. this.space = new cp.Space();
  157. var sprite = this.createPhysicsSprite( cc.p(cc.director.getWinSize().width/ , cc.director.getWinSize().height-) );
  158. this.addChild( sprite, );
  159.  
  160. this.addWalls();
  161. this.space.gravity = cp.v(, -);
  162. },
  163. initPhysics:function() {
  164. var space = this.space ;
  165. var staticBody = space.staticBody;
  166. var winSize = cc.director.getWinSize();
  167.  
  168. // Walls
  169. var walls = [ new cp.SegmentShape( staticBody, cp.v(,), cp.v(winSize.width,), ), // bottom
  170. new cp.SegmentShape( staticBody, cp.v(,winSize.height), cp.v(winSize.width,winSize.height), ), // top
  171. new cp.SegmentShape( staticBody, cp.v(,), cp.v(,winSize.height), ), // left
  172. new cp.SegmentShape( staticBody, cp.v(winSize.width,), cp.v(winSize.width,winSize.height), ) // right
  173. ];
  174. for( var i=; i < walls.length; i++ ) {
  175. var shape = walls[i];
  176. shape.setElasticity();
  177. shape.setFriction();
  178. space.addStaticShape( shape );
  179. }
  180.  
  181. // Gravity
  182. space.gravity = cp.v(, -);
  183. },
  184. addWalls:function() {
  185. // Walls
  186. var winSize = cc.director.getWinSize();
  187. var walls = [ new cp.SegmentShape( this.space.staticBody, cp.v(,), cp.v(winSize.width,), ), // bottom
  188. new cp.SegmentShape( this.space.staticBody, cp.v(,winSize.height), cp.v(winSize.width,winSize.height), ), // top
  189. new cp.SegmentShape( this.space.staticBody, cp.v(,), cp.v(,winSize.height), ), // left
  190. new cp.SegmentShape( this.space.staticBody, cp.v(winSize.width,), cp.v(winSize.width,winSize.height), ) // right
  191. ];
  192. for( var i=; i < walls.length; i++ ) {
  193. var shape = walls[i];
  194. shape.setElasticity(0.8);
  195. shape.setFriction(0.1);
  196. this.space.addStaticShape( shape );
  197. }
  198. },
  199. createPhysicsSprite:function( pos ) {
  200.  
  201. var radius = ;
  202. var mass = ;
  203.  
  204. var body = new cp.Body(mass, cp.momentForCircle(mass, , radius,cp.v(, )));
  205. body.setPos( pos );
  206. this.space.addBody( body );
  207. var shape = new cp.CircleShape(body, radius,cp.v(, )); //new cp.BoxShape( body, 48, 108);
  208. shape.setElasticity( );
  209. shape.setFriction( 0.1 );
  210.  
  211. this.space.addShape( shape );
  212.  
  213. var sprite = cc.PhysicsSprite.create(res.b_ball_01);
  214. sprite.setBody( body );
  215. return sprite;
  216. },
  217. setupDebugNode:function()
  218. {
  219. // debug only
  220. this._debugNode = cc.PhysicsDebugNode.create( this.space );
  221. this._debugNode.visible = false ;
  222. this.addChild( this._debugNode );
  223. }
  224. });
  225.  
  226. var MainScene = cc.Scene.extend({
  227. onEnter:function () {
  228. this._super();
  229. this.addChild(new GameBackgroundLayer());
  230. this.addChild(new MainLayer());
  231. }
  232. });

[cocos2d-js]chipmunk例子(二)的更多相关文章

  1. 【高德地图API】从零开始学高德JS API(二)地图控件与插件——测距、圆形编辑器、鼠标工具、地图类型切换、鹰眼鱼骨

    原文:[高德地图API]从零开始学高德JS API(二)地图控件与插件——测距.圆形编辑器.鼠标工具.地图类型切换.鹰眼鱼骨 摘要:无论是控件还是插件,都是在一级API接口的基础上,进行二次开发,封装 ...

  2. qrcode.js 动态生成二维码

    用qrcode.js动态生成二维码图片非常简单,只需要引入qrcode.js即可使用,而且可以自定义图片大小.背景色等信息. 1.jsp代码---页面头部引入qrcode.js,jquery文件可选 ...

  3. node.js 初学(二)—— 搭建注册/登录服务器

    node.js 初学(二)—— 搭建注册/登录服务器 理论上来说,代码实现在理论和实际上是一样的.但实际上来说,他们不是 做一个最简单的用户注册登录功能 1.接口定义: 注册:/user?act=re ...

  4. js生成简单二维码

    js文件下载地址:https://download.csdn.net/download/weixin_38296752/10554485 一.引入qrcode.js文件 <script type ...

  5. 进击Node.js基础(二)

    一.一个牛逼闪闪的知识点Promise npm install bluebird 二.Promise实例 ball.html <!doctype> <!DOCTYPE html> ...

  6. js生成中文二维码

    http://www.cnblogs.com/xcsn/archive/2013/08/14/3258035.html http://www.jb51.net/article/64928.htm 使用 ...

  7. cocos2d js jsb XMLHttpRequest 中文乱码

    1.首先讲下怎样使用XMLHttpRequest 下面所说的是在cocos2d-x 2.2.2 或者 2.3 版本号中. 首先要明确cocos2d js事实上分两个版本号,一个是html5的版本号,另 ...

  8. cocos2d js的一些tip

    cocos2d-js-v3.2-rc0 cc.director.end();//退出app cc.Application.getInstance().openURL("http://www. ...

  9. cocos2d js ClippingNode 制作标题闪亮特效

    1.效果图: 之前在<Android 高仿 IOS7 IPhone 解锁 Slide To Unlock>中制作了文字上闪亮移动的效果,这次我们来看下怎样在cocos2d js 中做出类似 ...

  10. Cordova app 检查更新 ----JS进行调用(二)

    原文:Cordova app 检查更新 ----JS进行调用(二) 1.获取版本号 需要添加 插件 cordova plugin add https://github.com/whiteoctober ...

随机推荐

  1. asp.net页面过滤所有换行符和多余空格

    不知道大家注意到了没有,Google和Baidu网页的HTML源代码是混合在一起的.HTML代码混合在一起,出发点是为了减小网页体积,从而加快网页加载速度. 写个函数把网页HTML源代码的换行符和空格 ...

  2. 标准模板库(STL)学习探究之stack

    标准模板库(STL)学习探究之stack queue priority_queue list map/multimap dequeue string

  3. ACM - ICPC World Finals 2013 H Матрёшка

    原题下载:http://icpc.baylor.edu/download/worldfinals/problems/icpc2013.pdf 题目翻译: 问题描述 俄罗斯套娃是一些从外到里大小递减的传 ...

  4. java连接mysql的一个小例子

    想要用java 连接数据库,需要在classpath中加上jdbc的jar包路径 在eclipse中,Project的properties里面的java build path里面添加引用 连接成功的一 ...

  5. SecureCRT介绍、安装、使用

    简介 SecureCRT是一款支持SSH(SSH1和SSH2)的终端仿真程序,简单地说是Windows下登录UNIX或Linux服务器主机的软件. SecureCRT支持SSH,同时支持Telnet和 ...

  6. HDU 1851 (巴什博奕 SG定理) A Simple Game

    这是由n个巴什博奕的游戏合成的组合游戏. 对于一个有m个石子,每次至多取l个的巴什博奕,这个状态的SG函数值为m % (l + 1). 然后根据SG定理,合成游戏的SG函数就是各个子游戏SG函数值的异 ...

  7. 漫游Kafka实战篇之客户端API

    Kafka Producer APIs 旧版的Procuder API有两种:kafka.producer.SyncProducer和kafka.producer.async.AsyncProduce ...

  8. ASP.NET 4的Demo实践:URL路由改进支持

    从.NET框架3.5 SP1开始,微软推出了ASP.NET路由支持,从而实现了特定资源的URL与其对应的Web服务器上的物理文件之间的彻底解耦.借助于ASP.NET路由支持,开发人员可以定义一组路由规 ...

  9. 指针数组vs数组指针 指针函数vs函数指针

    在分辨这些重要的概念时,我们先回顾一下前面所讲的C之三值合一,由于三个值所求出的地址是相同的,所以经常有传言说他们都是首元素的地址.这种说法是不正确的.为什么说它是不正确的呢? 首先定义一个指针,将三 ...

  10. 【英语】Bingo口语笔记(45) - Pass系列