引擎版本 —— cocos creator 2.3.4
游戏代码:
//优先初始化的时候,获取到cc.PhysicsPolygonCollider
this._poly = this.dragonFooAni.node.getComponent(cc.PhysicsPolygonCollider);
//下面的修改顶点位置的方法可以在update里面去执行
//获取骨骼动画上的挂件 str1:动画的父节点 str2:动画
let attachment = this.skAni.getAttachment('pengzhuang2', 'pengzhuang3');
//获取骨骼
let slot = this.skAni.findSlot('pengzhuang2');
//获取顶点的位置
let arr = [];
attachment.computeWorldVertices(slot, 0, attachment.worldVerticesLength, arr, 0, 2)
//console.log("多边形挂件:", attachment, "多边形顶点:", arr.toString());
for (let i = 0; i < 4; i++) {
this._poly.points[i].x = arr[i * 2];
this._poly.points[i].y = arr[i * 2 + 1];
}
this._poly.apply();
在原生环境下没有computeWorldVertices接口,我们需要复写引擎中的computeWorldVertices方法
_computeWorldVertices = function (slot, start, count, worldVertices, offset, stride) {
count = offset + (count >> 1) * stride;
let deformArray = slot.deform;
let vertices;
if (deformArray.length > 0)
vertices = deformArray;
if (!vertices)
return;
let bone = slot.bone;
let x = bone.worldX;
let y = bone.worldY;
let a = bone.a, b = bone.b, c = bone.c, d = bone.d;
for (let v_1 = start, w = offset; w < count; v_1 += 2, w += stride) {
let vx = vertices[v_1], vy = vertices[v_1 + 1];
worldVertices[w] = vx * a + vy * b + x;
worldVertices[w + 1] = vx * c + vy * d + y;
}
return;
};