laya的skeleton骨骼动画事件响应问题

    创建skeleton节点并绑定MOUSE_DOWN事件后,却始终无法响应。经测试发现如下:

    skeleton节点在load结束后,其bounds反映了总体的宽高,但是width与height却为0,而sprite(skeleton继承自sprite)却是以width与height来作为事件响应区域的。而单纯的sprite结点是没有这个问题的。因此将bounds.width与bounds.height重新赋值给skeleton节点,但是又出现了新的问题。

    调用nodeSkeleton.graphics.drawRect(0, 0, bounds.width, bounds.height, "#ff00ee"),绘制显示区域可以看到,Rect的左上角位置为(bounds.width/2, bounds.height),即在人物脚下中心线的位置。打开dragonbones编辑器,载入对应的动画,发现人物的root节点正是在脚下中心线的位置。也就是说,skeleton节点的动画区域是按照动画编辑器来设定的,与其Laya中的响应区域有很大的偏移。

    为了解决这个问题,创建了一个父节点proxy来响应事件:

var node = new Laya.Skeleton();
var proxy = new Laya.Sprite();
proxy.addChild(node);                
node.load(SkeletonConfigs["NuTao"], Laya.Handler.create(this, function() {
    var bound = node.getBounds(); // 加载完毕之后才能拿到有效的bounds
    var W = bound.width, H = bound.height;
    node.width = W;   // 若不设置的话, node.width与node.height均为0
    node.height = H;
    node.pos(W/2, H); // 骨骼节点偏移(W/2,H),使动画区域的左上角与proxy节点的位置(左上角)重合

    proxy.width = W;
    proxy.height = H;
    proxy.graphics.drawRect(0, 0, proxy.width, proxy.height, "#ff00ffee");
    proxy.on(Laya.Event.MOUSE_DOWN, this, function() {
        console.log("MouseDown Event");
    });
}));
原文地址:https://www.cnblogs.com/Jackie-Snow/p/8487607.html