最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Does EaselJS removeChild also remove attached eventListeners? - Stack Overflow

programmeradmin1浏览0评论

Does EaselJS's removeChild method, (part of Stage), handle cleanup of eventListeners attached to that child, as well? Or must you manually remove the child's event listeners using removeEventListener before removing the child?

For example:

stage = new createjs.Stage(canvas);
circle = new createjs.Shape();
circle.graphics.beginFill("#333").drawCircle(0,0,5);

circle.addEventListener("mousedown",function(event){
    console.log("mouse down");
});
stage.addChild(circle);
.
.
.
stage.removeChild(circle);

Does EaselJS's removeChild method, (part of Stage), handle cleanup of eventListeners attached to that child, as well? Or must you manually remove the child's event listeners using removeEventListener before removing the child?

For example:

stage = new createjs.Stage(canvas);
circle = new createjs.Shape();
circle.graphics.beginFill("#333").drawCircle(0,0,5);

circle.addEventListener("mousedown",function(event){
    console.log("mouse down");
});
stage.addChild(circle);
.
.
.
stage.removeChild(circle);
Share Improve this question edited May 25, 2021 at 15:20 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Aug 29, 2013 at 15:51 jzweigjzweig 111 silver badge2 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

EventListeners are not removed upon removeChild(), as they are also not added upon addChild() - If you do want to quickly remove them all, the quickest way would be myChild.removeAllEventListeners();

However if a DisplayObject is not attached to the Stage somehow, events won't be dispatched by mouse-interactions, because those only bubble through the stage and its' children.

In case you worry about memory-leaks: Events in EaselJS are sitting directly on the DisplayObject itself, so whenever you delete the reference to that Object, the Events will(should) be collected by the GarbageCollecter as well, no need for removing events separately. (please, someone correct me here, in case I'm wrong)

发布评论

评论列表(0)

  1. 暂无评论