HitTest.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. let _textureIdMapRenderTexture = {}
  2. cc.Class({
  3. extends: cc.Component,
  4. properties: {
  5. },
  6. // LIFE-CYCLE CALLBACKS:
  7. onLoad() {
  8. this.node._hitTest = this.hitTest.bind(this)
  9. },
  10. start() {
  11. },
  12. hitTest(location) {
  13. let spriteFrame = this.node.getComponent(cc.Sprite).spriteFrame
  14. if (spriteFrame == null) {
  15. return false
  16. }
  17. let posInNode = this.node.convertToNodeSpaceAR(location)
  18. let rect = spriteFrame.getRect()
  19. let offset = spriteFrame.getOffset()
  20. // var type = this.node.getComponent("DecorateItemPrefab").m_type
  21. // cc.log(type + "--", "xxxxxxxxxxxxxx", type)
  22. // cc.log(type + "--", "posInNode", posInNode)
  23. // cc.log(type + "--", "rect", rect)
  24. // cc.log(type + "--", "offset", offset)
  25. if ((posInNode.x < offset.x - rect.width / 2) || (posInNode.y < offset.y - rect.height / 2)
  26. || (posInNode.x > (offset.x + rect.width / 2)) || (posInNode.y > (offset.y + rect.height / 2))) {
  27. return false
  28. }
  29. else {
  30. let posInRect = cc.v2(parseInt(posInNode.x - offset.x + rect.width / 2), parseInt(posInNode.y - offset.y + rect.height / 2))
  31. // cc.log(type + "--", "posInRect", posInRect)
  32. // cc.log(type + "--", "isRotated", spriteFrame.isRotated())
  33. let tex = spriteFrame.getTexture()
  34. cc.log('test', tex instanceof (cc.RenderTexture));
  35. var rt;
  36. if (tex instanceof (cc.RenderTexture)) {
  37. rt = tex;
  38. } else {
  39. rt = _textureIdMapRenderTexture[tex.getId()];
  40. }
  41. if (!rt) {
  42. rt = new cc.RenderTexture()
  43. rt.initWithSize(tex.width, tex.height)
  44. rt.drawTextureAt(tex, 0, 0)
  45. _textureIdMapRenderTexture[tex.getId()] = rt
  46. }
  47. // data就是这个texture的rgba值数组
  48. let data
  49. if (spriteFrame.isRotated()) {
  50. data = rt.readPixels(null, rect.x + posInRect.y, rect.y + posInRect.x, 1, 1)
  51. // cc.log(type + "--", "data", data, rect.x + posInRect.y, rect.y + posInRect.x)
  52. }
  53. else {
  54. data = rt.readPixels(null, rect.x + posInRect.x, rect.y + rect.height - posInRect.y, 1, 1)
  55. // cc.log(type + "--", "data", data, rect.x + posInRect.x, rect.y + rect.height - posInRect.y)
  56. }
  57. // console.log("值是", data[3]);
  58. //if ((data[0] <= 0 && data[1] <= 0 && data[2] <= 0) || data[3] <= 0) {
  59. if (data[3] <= 0) {
  60. return false
  61. }
  62. else {
  63. return true
  64. }
  65. }
  66. },
  67. onDestroy() {
  68. let spriteFrame = this.node.getComponent(cc.Sprite).spriteFrame
  69. if (spriteFrame == null) {
  70. return false
  71. }
  72. let tex = spriteFrame.getTexture()
  73. let id = tex.getId()
  74. if (_textureIdMapRenderTexture[id]) {
  75. _textureIdMapRenderTexture[id].destroy()
  76. delete _textureIdMapRenderTexture[id]
  77. }
  78. }
  79. });