role.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // 角色类,管理各个动作
  2. var Serve = require("serve")
  3. var Pat = require("pat")
  4. var Idle = require("idle")
  5. var Peel = require("peel")
  6. var Fail = require("fail")
  7. var Win = require("win")
  8. // 角色的方向
  9. var RoleDir = cc.Enum ({
  10. Role_Mine_Left: 1,
  11. Role_Mine_Right: 2,
  12. Role_Rival_Left: 3,
  13. Role_Rival_Right: 4
  14. });
  15. cc.Class({
  16. extends: cc.Component,
  17. properties: {
  18. // 发球组件
  19. serve: {
  20. default: null,
  21. type: Serve,
  22. },
  23. // 接球组件
  24. pat: {
  25. default: null,
  26. type: Pat,
  27. },
  28. // 等待组件
  29. idle: {
  30. default: null,
  31. type: Idle,
  32. },
  33. // 削球组件
  34. peel: {
  35. default: null,
  36. type: Peel,
  37. },
  38. // 失败组件
  39. fail: {
  40. default: null,
  41. type: Fail,
  42. },
  43. // 胜利组件
  44. win: {
  45. default: null,
  46. type: Win,
  47. },
  48. },
  49. // LIFE-CYCLE CALLBACKS:
  50. onLoad () {
  51. this.initBlocks();
  52. this.initValues();
  53. },
  54. start () {
  55. this.idle.idle();
  56. },
  57. // update (dt) {},
  58. // 初始化
  59. // 初始化数据
  60. initValues:function() {
  61. this.mineLeft = cc.v2(-137, -176);
  62. this.mineRight = cc.v2(29, -176);
  63. this.rivalLeft = cc.v2(-10, 180);
  64. this.rivalRight = cc.v2(138, 180);
  65. },
  66. // 初始化各个组件的回调
  67. initBlocks:function () {
  68. var self = this;
  69. // 开始发球的回调
  70. this.serve.actionStart = function() {
  71. if (self.serveStart != null) {
  72. self.serveStart();
  73. }
  74. }
  75. var endBack = function() {
  76. self.idle.idle();
  77. };
  78. // 发球结束的回调
  79. this.serve.actionEnd = endBack;
  80. // 接球结束的回调
  81. this.pat.actionEnd = endBack;
  82. // 削球结束的回调
  83. this.peel.actionEnd = endBack;
  84. },
  85. // 外部调用
  86. // 设置位置
  87. setDir:function(dir) {
  88. if (dir == RoleDir.Role_Mine_Left) {
  89. this.node.setPosition(this.mineLeft);
  90. } else if (dir == RoleDir.Role_Mine_Right) {
  91. this.node.setPosition(this.mineRight);
  92. } else if (dir == RoleDir.Role_Rival_Left) {
  93. this.node.setPosition(this.rivalLeft);
  94. } else if (dir == RoleDir.Role_Rival_Right) {
  95. this.node.setPosition(this.rivalRight);
  96. }
  97. },
  98. // 发球
  99. roleServe:function() {
  100. this.serve.serve();
  101. },
  102. // 停止
  103. stop:function() {
  104. this.serve.stop();
  105. this.pat.stop();
  106. this.peel.stop();
  107. },
  108. // 角色的帧动作
  109. roleUpdate:function() {
  110. },
  111. });