Distance.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // Learn cc.Class:
  2. // - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
  3. // - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
  4. // Learn Attribute:
  5. // - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
  6. // - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
  7. // Learn life-cycle callbacks:
  8. // - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
  9. // - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html
  10. cc.Class({
  11. extends: cc.Component,
  12. properties: {
  13. Hero: {
  14. // ATTRIBUTES:
  15. default: null, // The default value will be used only when the component attaching
  16. // to a node for the first time
  17. type: cc.Node, // optional, default is typeof default
  18. serializable: true, // optional, default is true
  19. },
  20. layout : {
  21. default: null, // The default value will be used only when the component attaching
  22. // to a node for the first time
  23. type: cc.Node
  24. },
  25. tagDistance : false,
  26. //0 跳远 1 标枪
  27. type : 0,
  28. // bar: {
  29. // get () {
  30. // return this._bar;
  31. // },
  32. // set (value) {
  33. // this._bar = value;
  34. // }
  35. // },
  36. },
  37. // LIFE-CYCLE CALLBACKS:
  38. onLoad : function () {
  39. this.count = this.layout.getChildByName("layoutcount").getChildByName("count");
  40. this.layoutcount = this.layout.getChildByName("layoutcount");
  41. this.updateNum = 0;
  42. this.layoutcount.active = false;
  43. this.offsets = [
  44. 2.36,0.72
  45. ];
  46. },
  47. // start () {
  48. //
  49. // },
  50. startDistance : function (type,b) {
  51. this.type = type;
  52. this.tagDistance = b;
  53. var p = this.Hero.getPosition();
  54. if (!b) {
  55. this.updateNum = 0;
  56. // CustomLog("记录位置 结束 X",p.x);
  57. setTimeout(function () {
  58. this.layoutcount.active = false;
  59. if (this.callBack!=null) {
  60. var Label = this.count.getComponent("cc.Label");
  61. this.callBack(Label.string);
  62. }
  63. }.bind(this),3000);
  64. }
  65. else{
  66. // CustomLog("记录位置",p.x);
  67. this.defaultX = p.x;
  68. this.layoutcount.active = true;
  69. }
  70. },
  71. callBack : function () {
  72. },
  73. /**
  74. * 谁想知道结果 就谁调用这个方法 获取距离的方法
  75. * @param callBack
  76. */
  77. setCallBack : function (callBack) {
  78. this.callBack = callBack;
  79. },
  80. update : function (dt) {
  81. if (this.tagDistance) {
  82. var p = this.Hero.getPosition();
  83. // CustomLog("记录位置",p.x);
  84. if (this.defaultX != null) {
  85. this.calculationX = p.x - this.defaultX;
  86. var showX = this.calculationX/100/this.offsets[this.type];
  87. var Label = this.count.getComponent("cc.Label");
  88. Label.string = showX.toFixed(2);
  89. CustomLog("偏移量 X",showX);
  90. }
  91. if (this.updateNum == 0) {
  92. // CustomLog("记录位置 开始 X",p.x);
  93. }
  94. this.updateNum++;
  95. }
  96. }
  97. });