testAph.rar (735.5 KB)
这个demo是论坛上的,2.4.4版本,探索了一波都没法将他升级到3.5.2,哪位路过的大佬可以看看?
主要代码如下:
hitTest(location) {
console.log("location:", location);
let sf = this.node.getComponent(cc.Sprite).spriteFrame;
if (sf == null) {
return false;
}
let node = this.node;
//坐标转换
let posInNode = this.node.convertToNodeSpaceAR(location);
var size = node.getContentSize();
let rect = sf.getRect();
let offset = sf.getOffset();
//如果点击位置超出图片范围,则无效
if (Math.abs(posInNode.x) > size.width / 2 || Math.abs(posInNode.y) > size.height / 2) {
return false;
} else {
if (!this.camera)
this.camera = this.node.parent.getChildByName("check_camera").getComponent(cc.Camera);
if (!this.camera) return;
this.camera.node.active = true;
let posInRect = cc.v2(parseInt(posInNode.x - offset.x + rect.width / 2), parseInt(posInNode.y - offset.y + rect.height / 2));
let tex = sf.getTexture();
let data;
this.camera.enable = true;
let destory = false;
let rt=null;
if (tex instanceof cc.RenderTexture) {
this.node.group = "UI";
rt = tex;
} else {
this.node.group = "camera";
rt = new cc.RenderTexture();
if (tex.width == 2048) {
console.log("@@@@@@@@@@@@@@@@@@@@@@@" + this.node.name);
}
rt.initWithSize(tex.width, tex.height, cc.gfx.RB_FMT_S8);
rt.drawTextureAt(tex, 0, 0);
destory = true;
}
// data 就是这个texture的rgba值数组
this.camera.targetTexture = rt;
this.camera.render(undefined);
//图集里的图片可能会旋转
if (sf.isRotated()) {
data = rt.readPixels(null, rect.x + posInRect.y, rect.y + posInRect.x, 1, 1);
} else {
data = rt.readPixels(null, rect.x + posInRect.x, rect.y + rect.height - posInRect.y, 1, 1);
}
this.camera.targetTexture = null;
this.camera.enable = false;
this.camera.node.active = false;
this.node.group = "UI";
destory && rt.destroy();
if (data[3] <= 200) {
return false;
} else {
console.log("hitTest");
return true;
}
}
},