我在使用 Adobe Animate CC 及其使用的 Javascript 框架 EaselJS/CreateJS 时遇到问题。我可以添加事件监听器而不会出现问题,但无法删除它们。我已经在调试器中尝试了各种操作,包括步入其 JS removeEventListener 处理程序 - 尽管这两个变量看起来相同,但它们永远不会 == (或 ===),因此偶数监听器不会被删除。
这是我使用的代码,其目的是在将鼠标悬停在元素上时淡入该元素:
this.fadeIn = function(target_mc)
{
target_mc.alpha = 0;
target_mc.visible = true;
target_mc.removeEventListener("tick",fadeIn_onEnterFrame);
target_mc.addEventListener("tick",fadeIn_onEnterFrame.bind(this));
function fadeIn_onEnterFrame(evt)
{
evt.currentTarget.alpha = evt.currentTarget.alpha + .2;
if (evt.currentTarget.alpha >= 1)
{
evt.currentTarget.removeEventListener("tick",fadeIn_onEnterFrame);
} // end if
}
} // End of the function
所以你知道,它被添加到frame_0中的 Canvas 中,并从添加到每种服装类型的“鼠标悬停”监听器中调用(它用于拖放装扮游戏,FWIW)
function clothing_onRollOver()
{
this.hint_mc.desc_mc.desc1_txt.text = this.articleName;
this.fadeIn(this.hint_mc);
this.clothingOver = true;
};
clothing.addEventListener("mouseover",clothing_onRollOver.bind(this));
最佳答案
您的代码似乎存在一些范围问题。下面的代码片段修复了您的问题并正确删除了事件监听器,但是我不知道为什么您当前的实现无法正常工作。
不同之处在于我已从 target_mc.addEventListener("tick",fadeIn_onEnterFrame.bind(this));
但是我不确定为什么 .bind()
会导致此问题。
stage.enableMouseOver();
clothingOver = false;
this.hint_mc.visible = false;
this.fadeIn = function(target_mc)
{
console.log(target_mc);
target_mc.alpha = 0;
target_mc.visible = true;
target_mc.removeEventListener("tick",fadeIn_onEnterFrame);
target_mc.addEventListener("tick",fadeIn_onEnterFrame);
function fadeIn_onEnterFrame(evt)
{
console.log("tick");
evt.currentTarget.alpha = evt.currentTarget.alpha + .2;
if (evt.currentTarget.alpha >= 1)
{
evt.currentTarget.removeEventListener("tick", fadeIn_onEnterFrame);
} // end if
}
} // End of the function
function clothing_onRollOver()
{
//this.hint_mc.desc_mc.desc1_txt.text = this.articleName;
this.fadeIn(this.hint_mc);
this.clothingOver = true;
};
this.clothing.addEventListener("mouseover",clothing_onRollOver.bind(this));
抱歉,我无法为您确定确切的根本原因。
关于javascript - Animate/EaselJS - removeEventListener 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41816274/