ClickTest.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. let _textureIdMapRenderTexture = {}
  2. cc.Class({
  3. extends: cc.Component,
  4. properties: {
  5. },
  6. start() {
  7. // this.node.on(cc.Node.EventType.TOUCH_END,this.btnClick,this);
  8. // this.node._hitTest = this.hitTest.bind(this);
  9. },
  10. hitTest2(location) {
  11. cc.log('location', location);
  12. let spriteFrame = this.node.getComponent(cc.Sprite).spriteFrame
  13. if (spriteFrame == null) {
  14. return false
  15. }
  16. let posInNode = this.node.parent.convertToNodeSpaceAR(location)
  17. let rect = spriteFrame.getRect()
  18. let offset = spriteFrame.getOffset()
  19. cc.log("--", "posInNode", posInNode)
  20. cc.log("--", "rect", rect)
  21. cc.log("--", "offset", offset)
  22. let tex = spriteFrame.getTexture();
  23. let rt = new cc.RenderTexture();
  24. rt.initWithSize(tex.width, tex.height);
  25. rt.drawTextureAt(tex, 0, 0);
  26. // data就是这个texture的rgba值数组
  27. let data = rt.readPixels(null,posInNode.x,posInNode.y,1,1);
  28. // console.log("值是", data);
  29. //if ((data[0] <= 0 && data[1] <= 0 && data[2] <= 0) || data[3] <= 0) {
  30. // if (data[3] <= 0) {
  31. // return false
  32. // }
  33. // else {
  34. // return true
  35. // }
  36. },
  37. hitTest(location) {
  38. cc.log('location', location);
  39. let spriteFrame = this.node.parent.getComponent(cc.Sprite).spriteFrame
  40. if (spriteFrame == null) {
  41. return false
  42. }
  43. let posInNode = this.node.parent.convertToNodeSpaceAR(location)
  44. let rect = spriteFrame.getRect()
  45. let offset = spriteFrame.getOffset()
  46. cc.log("--", "posInNode", posInNode)
  47. cc.log("--", "rect", rect)
  48. cc.log("--", "offset", offset)
  49. if ((posInNode.x < offset.x - rect.width / 2) || (posInNode.y < offset.y - rect.height / 2)
  50. || (posInNode.x > (offset.x + rect.width / 2)) || (posInNode.y > (offset.y + rect.height / 2))) {
  51. // console.log("值是false");
  52. return false
  53. }
  54. else {
  55. let posInRect = cc.v2(parseInt(posInNode.x - offset.x + rect.width / 2), parseInt(posInNode.y - offset.y + rect.height / 2))
  56. // cc.log(type + "--", "posInRect", posInRect)
  57. // cc.log(type + "--", "isRotated", spriteFrame.isRotated())
  58. let tex = spriteFrame.getTexture()
  59. var rt;
  60. if (tex instanceof (cc.RenderTexture)) {
  61. rt = tex;
  62. } else {
  63. rt = _textureIdMapRenderTexture[tex.getId()];
  64. }
  65. if (!rt) {
  66. rt = new cc.RenderTexture()
  67. rt.initWithSize(tex.width, tex.height)
  68. rt.drawTextureAt(tex, 0, 0)
  69. _textureIdMapRenderTexture[tex.getId()] = rt
  70. }
  71. // data就是这个texture的rgba值数组
  72. let data
  73. if (spriteFrame.isRotated()) {
  74. data = rt.readPixels(null, rect.x + posInRect.y, rect.y + posInRect.x, 1, 1)
  75. // cc.log(type + "--", "data", data, rect.x + posInRect.y, rect.y + posInRect.x)
  76. }
  77. else {
  78. data = rt.readPixels(null, rect.x + posInRect.x, rect.y + rect.height - posInRect.y, 1, 1)
  79. // cc.log(type + "--", "data", data, rect.x + posInRect.x, rect.y + rect.height - posInRect.y)
  80. }
  81. // console.log("值是", data[3]);
  82. //if ((data[0] <= 0 && data[1] <= 0 && data[2] <= 0) || data[3] <= 0) {
  83. if (data[3] <= 0) {
  84. return false
  85. }
  86. else {
  87. return true
  88. }
  89. }
  90. },
  91. onDestroy() {
  92. let spriteFrame = this.node.getComponent(cc.Sprite).spriteFrame
  93. if (spriteFrame == null) {
  94. return false
  95. }
  96. let tex = spriteFrame.getTexture()
  97. let id = tex.getId()
  98. if (_textureIdMapRenderTexture[id]) {
  99. _textureIdMapRenderTexture[id].destroy()
  100. delete _textureIdMapRenderTexture[id]
  101. }
  102. }
  103. },
  104. // update (dt) {},
  105. );