Cocos2d-html5《王者之剑》实现 (1)

前面,《 手把手,快速搭建 Cocos2d-HTML5 开发调试环境》 与 《 如何自定义 Cocos2d-HTML5 Loading 界面》 两篇文章,帮助了我们搭建了其开发环境,并了解了 H5 ( 以下对 Cocos2d-HTML5 简称 H5,显然,它不是 * 流感,没那么大破坏力) 的大致加载流程,此文开始就要使用 H5 来制作一个简单的动作游戏 王者之剑,完成效果如下所示(所有的源码你可以在 【 这里】查看),显示效果可以在 【 这里】查看(请耐心等待加载):
 
这是一个简单的游戏打斗场景,一个英雄,一个怪物,可以控制英雄来回走动并且攻击,怪物实现简单 AI 并且自动攻击,有着不同的血量槽,控制系统,可以使用触摸,但为了操作的体验,同样实现了 键盘映射 ,可以使用 W、A、S、D 来控制人物的走动,J、U、I 实现一个普通攻击和两个特效攻击。
 
项目组织
为了使项目的代码结构清晰,前期规划好功能是必须的,先从整体看一下,项目的组织结构,然后会对其中内部实现做些必要的解说:
如上所示, Arthur 为游戏项目的主目录,与它同级的目录,是 H5 的库目录,当然截图中,为了发布,删除了一些不必要的文件,在 Arthut 目录下,包含一般项目都包含的结构组织。
 
index.html : 这是游戏的展示界面,其中包含 “gameCanvas” ,作为游戏绘制的所在,它引用加载了 cocos2d.js
cocos2d.js : 项目初始化在这里进行,并完成系统库和项目源码的 js 加载,最后将控制权交给 main.js 文件
main.js : 当 H5 库加载完毕,执行内中代码,完成项目资源加载,并运行第一个场景
src : 此目录包含了游戏中编写的 js 源代码文件
res : 游戏所需的资源,如图片,字体等
 
在这个游戏中相对复杂一点的就是控制系统了, HudLayer 中添加实现了 ActionButton 普通攻击按钮, Joypad 可触摸 360 度 摇杆功能,和 KeyMap 游戏控制键盘映射方案。Characters 实现了人物和怪物的功能,各种动作控制。Loading 替换了 H5 的默认加载界面,使用了一个进度条显示加载进度。GameLayer 作为游戏的主场景,各种游戏的流程控制在这里进行。
 
360 度 可触摸摇杆实现
这里的摇杆,默认是为了触摸实现,之后添加的键盘映射,只是为了让操作更为方便而已(在 PC 浏览器中),触摸不同于摇杆的所在,就是这里的摇杆可以是 360 度以内的任意角度,也就是可以控制任意以任意方向移动,这是键盘所不具备的,上下左右四个键,再加上每两个方向的组合也就八个方向。
 
 
 
  1. var Joypad = cc.Layer.extend({ 
  2. _winSize: null
  3. _pCenter: null
  4. _pControlSprite: null
  5. _pDefaultPoint: null
  6.   
  7. _pDefaultRotation: null
  8. _pRotation: null
  9.   
  10. _pDelegate: null
  11. _pKeyDown: false
  12. ctor:function(){ 
  13.  this._super(); 
  14.   
  15. _winSize = cc.Director.getInstance().getWinSize(); 
  16. _pCenter = cc.p(_winSize.width / 2, _winSize.height / 2); 
  17.   
  18.  }, 
  19. init:function(){ 
  20.  var bRet = false
  21.  if (this._super()){ 
  22. cc.log("Joypad init .."); 
  23.  // 控制杆所在位置 
  24.  this._pDefaultPoint = cc.p(110, 110); 
  25.  // 默认旋转角度,以使开口正对右侧 
  26.  this._pDefaultRotation = 26; 
  27.  // 实际旋转角度 
  28.  this._pRotation = 0; 
  29.   
  30.  this.setPosition(this._pDefaultPoint); 
  31.   
  32.  this.addChild(cc.Sprite.create(s_Joypad1)); 
  33.  this.addChild(cc.Sprite.create(s_Joypad2)); 
  34.  this._pControlSprite = cc.Sprite.create(s_Joypad3); 
  35.  this.addChild(this._pControlSprite); 
  36.  this.addChild(cc.Sprite.create(s_Joypad4)); 
  37.   
  38.  this.updateRotation(); 
  39.   
  40. bRet = true
  41.  } 
  42.  return bRet; 
  43.  }, 
  44. keyStart:function(degrees){ 
  45.  if (this._pDelegate) 
  46.  this._pDelegate.actionJoypadStart(this._pRotation); 
  47.  }, 
  48. keyUpdate:function(degrees){ 
  49.  this._pRotation = degrees; 
  50.  this.updateRotation(); 
  51.  if (this._pDelegate) 
  52.  this._pDelegate.actionJoypadUpdate(this._pRotation); 
  53.  }, 
  54. keyEnded:function(degrees){ 
  55.  if (this._pDelegate) 
  56.  this._pDelegate.actionJoypadEnded(this._pRotation); 
  57.  }, 
  58. onEnter:function(){ 
  59.  this._super(); 
  60. cc.Director.getInstance().getTouchDispatcher().addTargetedDelegate(this, 0, true); 
  61.  }, 
  62. onTouchBegan:function (touch, event){ 
  63.  // 点击点的范围判断 
  64.  var curPoint = touch.getLocation(); 
  65.  if (curPoint.x > _winSize.width / 2 || curPoint.y > _winSize.height / 2 ){ 
  66.  return false
  67.  } 
  68.   
  69.  // var sp = cc.pSub(this._pDefaultPoint, curPoint); 
  70.  // var angle = cc.pToAngle(sp); 
  71.   
  72.  this.updateTouchRotation(touch, event); 
  73.  this.updateRotation(); 
  74.  if(this._pDelegate) 
  75.  this._pDelegate.actionJoypadStart(this._pRotation); 
  76.  else 
  77. cc.log('_pDelegate is null ... '); 
  78.   
  79.  // cc.log("Joypad touch ..."); 
  80.  return true
  81.  }, 
  82. onTouchMoved:function (touch, event){ 
  83.  this.updateTouchRotation(touch, event); 
  84.  this.updateRotation(); 
  85.   
  86.  if (this._pDelegate) 
  87.  this._pDelegate.actionJoypadUpdate(this._pRotation); 
  88.  else 
  89. cc.log('_pDelegate is null ... '); 
  90.   
  91.  // var a = cc.pAngleSigned( curPoint, this._pDefaultPoint); 
  92.  // cc.log("Joypad touch mvove ..." + rotation) ; 
  93.  }, 
  94. onTouchEnded:function (touch, event){ 
  95.  this.updateTouchRotation(touch, event); 
  96.  this.updateRotation(); 
  97.  if (this._pDelegate) 
  98.  this._pDelegate.actionJoypadEnded(this._pRotation); 
  99.  else 
  100. cc.log('_pDelegate is null ... '); 
  101.  }, 
  102. updateTouchRotation:function(touch, event){ 
  103.  var curPoint = touch.getLocation(); 
  104.  var sp = cc.pSub(curPoint, this._pDefaultPoint); 
  105.  var angle = cc.pToAngle(sp) ;// * -57.29577951; 
  106.  var rotation = angle * -57.29577951; 
  107. rotation = rotation < 0 ? 360 + rotation: rotation; 
  108.  this._pRotation = rotation; 
  109.  }, 
  110. updateRotation:function(){ 
  111.  this._pControlSprite.setRotation(this._pDefaultRotation + this._pRotation); 
  112.  }, 
  113. setDelegate:function(dg){ 
  114.  this._pDelegate = dg; 
  115.  } 
  116. }); 

在初始化方法中,加载了摇杆资源文件,它分解成几个组成部分,以便于很好的控制,并且保存了可旋转元素精灵的引用this._pControlSprite,以便于随时控制它的旋转角度,如图中 Joypad3.png 图片。
 
以触摸的动作来控制动作的执行,Joypad 中包含了一个名为 _pDelegate 的属性,它作为控制摇杆的代理,以通知其它 (如 人物),摇杆现在变动了,分别在 onTouchBegan 中调用, this._pDelegate.actionJoypadStart(this._pRotation);,onTouchMoved 中调用 this._pDelegate.actionJoypadUpdate(this._pRotation); 和在 onTouchEnded 中调用 this._pDelegate.actionJoypadEnded(this._pRotation);
 
只需要在传入的 _pDelegate 中实现此三种函数,就可以通过摇杆来控制其操作了,H5 使用 javascript 相比如 C++ 倒也省去了接口定义等繁杂的操作。可以看见,在三个函数调用中,所传入的参数为触摸的角度,在触摸是通知控制显示摇杆中 “罗盘” 的旋转。Joypad 对内通过触摸控制显示,对外通过触摸调用代理,以达到显示和控制相一致的目的。通过触摸的点相对摇杆原点的位置关系,很容计算出其角度。
 
由于这里的摇杆设计是 360 度任意角度,所以在 delegate 中传出一个参数,以标示角度关系,如果并不需要那么复杂的控制,如前文所言,只需固定八个方向的控制,那么这里传出的参数可以使用 枚举 类型,代表八个不同的方向,也会使得游戏逻辑变得稍微简单。
 
最后可以为 Joypad 层封装一个简单好用的调用方式:
 
 
 
  1. Joypad.create = function(){ 
  2.  var joypad = new Joypad(); 
  3.  if (joypad && joypad.init()){ 
  4.  return joypad; 
  5.  } 
  6.  return null
  7. }; 
 
攻击 与 特效攻击
在这个游戏中,有一个普通攻击和两个特效攻击,这两个不同,但很显然,他们都是攻击,却又相同,先看看他们的共同点:
 
 
  1. // ActionButton.js 
  2.   
  3. var ActionButton = cc.Node.extend({ 
  4. _sprite: null
  5. _rect: null
  6. _delegate: null
  7. _attackType: null
  8.   
  9. _childObj: null
  10. rect:function(){ 
  11.  var size = this._sprite.getContentSize(); 
  12.  return cc.rect(-size.width / 2, -size.height / 2, size.width, size.height); 
  13.  }, 
  14. setChindObj:function(obj){ 
  15.  this._childObj = obj; 
  16.  }, 
  17. init:function(image){ 
  18.  this._super(); 
  19.   
  20.  this._sprite = cc.Sprite.create(image); 
  21.  this.addChild(this._sprite); 
  22.  return true
  23.  }, 
  24. setDelegate:function(delegate){ 
  25.  this._delegate = delegate; 
  26.  }, 
  27. setAttackType:function(at){ 
  28.  this._attackType = at; 
  29.  }, 
  30. getAttackType:function(){ 
  31.  return this._attackType; 
  32.  }, 
  33. onEnter:function(){ 
  34. cc.Director.getInstance().getTouchDispatcher().addTargetedDelegate(this, 0, false); 
  35.  this._super(); 
  36.  }, 
  37. onExit:function(){ 
  38. cc.Director.getInstance().getTouchDispatcher().removeDelegate(this); 
  39.  this._super(); 
  40.  }, 
  41. containsTouchLocation:function(touch){ 
  42.  return cc.rectContainsPoint(this.rect(), this.convertTouchToNodeSpace(touch)); 
  43.  }, 
  44. onTouchBegan:function(touch, event){ 
  45.  // 区域判断 
  46.  if (!this.containsTouchLocation(touch)) 
  47.  return false
  48.  this.click(); 
  49.  // 播放点击动画 
  50.  return true
  51.  }, 
  52. click:function(){ 
  53.  if(this._delegate && this._childObj.isCanClick()){ 
  54.  this._delegate.attackButtonClick(this.getAttackType()); 
  55.  this.beganAnimation(); 
  56.  } 
  57.  }, 
  58. onTouchEnded:function(touch, event){ 
  59.  this.endedAnimation(); 
  60.  }, 
  61. beganAnimation:function(){ 
  62.  }, 
  63. endedAnimation:function(){ 
  64.  }, 
  65. isCanClick:function(){ 
  66.  return true
  67.  } 
  68. }); 
定义了一个 ActionButton 攻击按钮类型,它实现了 onTouchBegan 作为按钮点击的触发场所,触发了 click 事件,再由 click 处理调用代理的事件,传出一个参数,以标示攻击的类型 AttackType,在判断点击的时候还需要检测点击区域是否在按钮的可点击范围之内,当然再触发攻击动作之时,按钮本身也实现了一些动画特效,如点击效果,技能冷却效果,它由beganAnimation 方法实现。
 
但我们看见在 ActionButton 并没有实现 beganAnimation,在方法里面并没有实现任何代码,这因为 ActionButton 只是作为 攻击按钮 的抽象,它只定义了攻击按钮具体由那些功能,能做哪些事情,如可以播放点击时的动画,但具体的动画内容,需要根据具体的攻击按钮有着不同的实现。
 
 
 
  1. var AttackButton = ActionButton.extend({ 
  2. _pt: null
  3. _ac: null
  4.   
  5. _defaultScale: 0.35, 
  6. _maxScale: 0.5, 
  7.   
  8. _inAction: null
  9. _outAction: null
  10.   
  11. _timestamp: null
  12. ctor:function(){ 
  13.  this._super(); 
  14.  this._pt= cc.Sprite.create(s_AttackO); 
  15.  this._pt.setScale(this._maxScale); 
  16.  this.setChindObj(this); 
  17.   
  18.  // this.addChild(this._pt); 
  19.   
  20.  var aScale = cc.ScaleTo.create(0.1, this._defaultScale); 
  21.  var aFadein = cc.FadeIn.create(0.1); 
  22.  this._inAction = cc.Spawn.create(aScale, aFadein); 
  23.   
  24.  var oScale = cc.ScaleTo.create(.2, this._maxScale); 
  25.  var oFade = cc.FadeOut.create(0.2); 
  26.  this._outAction = cc.Spawn.create(oScale, oFade); 
  27.  }, 
  28. beganAnimation:function(){ 
  29.  var timestamp = (new Date()).valueOf(); 
  30.  this._timestamp = timestamp; 
  31.   
  32.  this.removeChild(this._pt); 
  33.  this.addChild(this._pt); 
  34.  this._pt.runAction(this._inAction); 
  35.   
  36.  }, 
  37. endedAnimation:function(){ 
  38.  this._pt.stopAllActions(); 
  39.  this._pt.runAction(this._outAction); 
  40.  }, 
  41. clickUp:function(){ 
  42.  this.endedAnimation(); 
  43.  }, 
  44. isCanClick:function(){ 
  45.  var timestamp = (new Date()).valueOf(); 
  46.  return timestamp - this._timestamp > 600; 
  47.  } 
  48. }); 
 
 
普通攻击按钮的效果,初始化设置图片素材,播放动画为一个光圈放大缩小显示,它 继承 自 ActionButton ,同样实现了 beganAnimation 方法。另外一种是特效攻击的实现:
 
 
 
  1. var AttackEffect = ActionButton.extend({ 
  2. _pt: null
  3. _ac: null
  4. _isCanClick: true
  5. ctor:function(){ 
  6.  this._super(); 
  7.  var h = cc.Sprite.create(s_AttackFreeze); 
  8.  this._pt = cc.ProgressTimer.create(h); 
  9.  this._pt.setType(cc.PROGRESS_TIMER_TYPE_RADIAL); 
  10.  this._pt.setReverseDirection(true); 
  11.  this._pt.setScale(0.43); 
  12.   
  13.  var to = cc.ProgressTo.create(0, 99.999); 
  14.  var to1 = cc.ProgressTo.create(2, 0); 
  15.  var ac2 = cc.CallFunc.create(this.callBack, this); 
  16.  this._ac = cc.Sequence.create(to, to1, ac2); 
  17.  this.setChindObj(this); 
  18.  }, 
  19. beganAnimation:function(){ 
  20.  this.removeChild(this._pt); 
  21.  this.addChild(this._pt); 
  22.  this._pt.runAction(this._ac); 
  23.  this._isCanClick = false
  24.  }, 
  25. endedAnimation:function(){ 
  26.  }, 
  27. callBack:function(){ 
  28.  // cc.log("call back"); 
  29.  this._isCanClick = true
  30.  }, 
  31. isCanClick:function(){ 
  32.  return this._isCanClick; 
  33.  } 
  34. }); 
 
特效攻击有个冷却效果,不能在一定时间范围内连续攻击,使用一个 旋转的 Progress 来达到这样的效果。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值