| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- let _textureIdMapRenderTexture = {}
- cc.Class({
- extends: cc.Component,
- properties: {
- },
- // LIFE-CYCLE CALLBACKS:
- onLoad() {
- this.node._hitTest = this.hitTest.bind(this)
- },
- start() {
- },
- hitTest(location) {
- let spriteFrame = this.node.getComponent(cc.Sprite).spriteFrame
- if (spriteFrame == null) {
- return false
- }
- let posInNode = this.node.convertToNodeSpaceAR(location)
- let rect = spriteFrame.getRect()
- let offset = spriteFrame.getOffset()
- // var type = this.node.getComponent("DecorateItemPrefab").m_type
- // cc.log(type + "--", "xxxxxxxxxxxxxx", type)
- // cc.log(type + "--", "posInNode", posInNode)
- // cc.log(type + "--", "rect", rect)
- // cc.log(type + "--", "offset", offset)
- if ((posInNode.x < offset.x - rect.width / 2) || (posInNode.y < offset.y - rect.height / 2)
- || (posInNode.x > (offset.x + rect.width / 2)) || (posInNode.y > (offset.y + rect.height / 2))) {
- return false
- }
- else {
- let posInRect = cc.v2(parseInt(posInNode.x - offset.x + rect.width / 2), parseInt(posInNode.y - offset.y + rect.height / 2))
- // cc.log(type + "--", "posInRect", posInRect)
- // cc.log(type + "--", "isRotated", spriteFrame.isRotated())
- let tex = spriteFrame.getTexture()
- cc.log('test', tex instanceof (cc.RenderTexture));
- var rt;
- if (tex instanceof (cc.RenderTexture)) {
- rt = tex;
- } else {
- rt = _textureIdMapRenderTexture[tex.getId()];
- }
- if (!rt) {
- rt = new cc.RenderTexture()
- rt.initWithSize(tex.width, tex.height)
- rt.drawTextureAt(tex, 0, 0)
- _textureIdMapRenderTexture[tex.getId()] = rt
- }
- // data就是这个texture的rgba值数组
- let data
- if (spriteFrame.isRotated()) {
- data = rt.readPixels(null, rect.x + posInRect.y, rect.y + posInRect.x, 1, 1)
- // cc.log(type + "--", "data", data, rect.x + posInRect.y, rect.y + posInRect.x)
- }
- else {
- data = rt.readPixels(null, rect.x + posInRect.x, rect.y + rect.height - posInRect.y, 1, 1)
- // cc.log(type + "--", "data", data, rect.x + posInRect.x, rect.y + rect.height - posInRect.y)
- }
- // console.log("值是", data[3]);
- //if ((data[0] <= 0 && data[1] <= 0 && data[2] <= 0) || data[3] <= 0) {
- if (data[3] <= 0) {
- return false
- }
- else {
- return true
- }
- }
- },
- onDestroy() {
- let spriteFrame = this.node.getComponent(cc.Sprite).spriteFrame
- if (spriteFrame == null) {
- return false
- }
- let tex = spriteFrame.getTexture()
- let id = tex.getId()
- if (_textureIdMapRenderTexture[id]) {
- _textureIdMapRenderTexture[id].destroy()
- delete _textureIdMapRenderTexture[id]
- }
- }
- });
|