RubCard.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. cc.Class({
  2. extends: cc.BaseClass,
  3. SetHook:function(Hook) {
  4. this.m_Hook = Hook;
  5. this.mask = this.$('CardPrefab');
  6. this.cardShow = this.$('CardPrefab/Card');
  7. this.cardBack = this.$('CardPrefab/BG');
  8. this.m_NdTouch = this.$('Touch');
  9. this.m_Card = this.$('CardPrefab@RubCardPrefab');
  10. this.CardVaule = this.$('CardPrefab/CardVaule');
  11. this.CardVaule2 = this.$('CardPrefab/CardVaule2');
  12. this.CardVaule.active = false;
  13. this.CardVaule2.active = false;
  14. this.worldPos = this.mask.convertToWorldSpaceAR(this.cardBack.getPosition());
  15. this.m_NdTouch.on(cc.Node.EventType.TOUCH_START,this.onTouchBegan.bind(this),this.m_NdTouch);
  16. this.m_NdTouch.on(cc.Node.EventType.TOUCH_MOVE,this.onTouchMoved.bind(this),this.m_NdTouch);
  17. this.m_NdTouch.on(cc.Node.EventType.TOUCH_END,this.onTouchEnded.bind(this),this.m_NdTouch);
  18. this.m_NdTouch.on(cc.Node.EventType.TOUCH_CANCEL,this.onTouchEnded.bind(this),this.m_NdTouch);
  19. },
  20. SetCardData :function(cbCardData){
  21. this.m_EndTime = 0;
  22. this.m_bTouched = true;
  23. this.CardVaule.active = false;
  24. this.CardVaule2.active = false;
  25. this.m_Card.SetData(cbCardData);
  26. //this.m_Card.SetValueShow(false);
  27. this.cardBack.active = true;
  28. this.mask.setPosition(cc.v2(0, 0));
  29. this.cardShow.setPosition(cc.v2(1024, 720));
  30. this.mask.anchorX = 0.5;
  31. this.mask.angle = 0;
  32. this.mask.rotation = -90;
  33. this._stay();
  34. },
  35. //触摸事件
  36. onTouchBegan:function(event){
  37. if( !this.m_bTouched ) return;
  38. this.startPos = event.getLocation();
  39. },
  40. onTouchMoved:function(event){
  41. if( !this.m_bTouched ) return;
  42. let pos = event.getLocation();
  43. pos = cc.v2(pos.x, pos.y);
  44. // 点击移动的偏移量
  45. let offset = pos.sub(this.startPos);
  46. if (offset.x >= 0) this.LeftOrRight = true;
  47. else if (offset.x < 0) this.LeftOrRight = false;
  48. else return;
  49. if (offset.y > 0) this.UpOrDown = false;
  50. else if (offset.y < 0) this.UpOrDown = true;
  51. else return;
  52. // console.log((this.LeftOrRight ? '左' : '右') + (this.UpOrDown ? '上' : '下'));
  53. // 转换成角上的位置偏移
  54. var AnchorX = 0;
  55. var AnchorY = 0;
  56. if (this.LeftOrRight) AnchorX = 1;
  57. if (this.UpOrDown) AnchorY = 1;
  58. this.m_Card.SetCardAnchor(AnchorX,AnchorY);
  59. let cardBackPointWorld = this.worldPos.add(cc.v2((this.LeftOrRight ? -1 : 1) * this.cardBack.width / 2,
  60. (this.UpOrDown ? 1 : -1) * this.cardBack.height / 2));
  61. let cardShowPointWorld = cardBackPointWorld.add(offset);
  62. // 两点间中点
  63. let midWorld = cardShowPointWorld.add(cardBackPointWorld);
  64. midWorld = cc.v2(midWorld.x / 2, midWorld.y / 2);
  65. let angle = Math.atan(offset.y / offset.x) / Math.PI * 180;
  66. angle = -angle;
  67. //设置mask位置, 为两点间中点
  68. this.mask.anchorX = this.LeftOrRight ? 0 : 1;
  69. let maskPos = this.mask.parent.convertToNodeSpaceAR(midWorld);
  70. this.mask.setPosition(maskPos);
  71. this.cardShow.setPosition(this.cardShow.parent.convertToNodeSpaceAR(cardShowPointWorld));
  72. this.mask.rotation = angle;
  73. this.cardShow.rotation = angle;
  74. this._stay();
  75. },
  76. onTouchEnded:function(event) {
  77. if( !this.m_bTouched ) return;
  78. let pos = event.getLocation();
  79. pos = cc.v2(pos.x, pos.y);
  80. // 点击移动的偏移量
  81. let offset = pos.sub(this.startPos);
  82. if (Math.abs(offset.x) > this.cardBack.width/2 || Math.abs(offset.y) > this.cardBack.height/2) {
  83. console.log('开牌');
  84. return this.OnShowCard();
  85. } else {
  86. this.mask.setPosition(cc.v2(0, 0));
  87. this.cardShow.setPosition(cc.v2(1024, 720));
  88. this.mask.anchorX = 0.5;
  89. this.mask.angle = 0;
  90. this._stay();
  91. }
  92. },
  93. // 保证底牌不动, 使用widget插件不行, 估计widget插件带上角度后有bug, 所以用代码控制
  94. _stay:function() {
  95. this.cardBack.rotation = -this.cardBack.parent.rotation;
  96. let cardBackPos = this.mask.convertToNodeSpaceAR(this.worldPos);
  97. this.cardBack.setPosition(cardBackPos)
  98. },
  99. OnShowCard:function() {
  100. this.m_bTouched = false;
  101. this.CardVaule.active = true;
  102. this.CardVaule2.active = true;
  103. //this.m_Card.SetValueShow(true);
  104. this.m_Card.SetCardAnchor(0.5,0.5);
  105. this.cardShow.setPosition(cc.v2(0, 0));
  106. this.cardShow.rotation = -90;
  107. this.cardBack.active = false;
  108. this.mask.setPosition(cc.v2(0, 0));
  109. this.mask.anchorX = 0.5;
  110. this.mask.rotation = 90;
  111. this._stay();
  112. this.m_EndTime = new Date().getTime()+1000;
  113. },
  114. update:function() {
  115. var Now = new Date().getTime();
  116. if(this.m_EndTime != 0 && Now >= this.m_EndTime){
  117. this.m_EndTime = 0;
  118. this.m_Hook.m_GameClientView.OnBnClickedShowCard();
  119. this.CardVaule.active = false;
  120. this.CardVaule2.active = false;
  121. }
  122. },
  123. });