悬停时更改 MC 的 alpha

发布于 2024-10-31 09:21:44 字数 306 浏览 0 评论 0原文

这看起来应该对我有用,但显然我做错了什么。我不知道到底是什么,这显然是一个新手在谷歌搜索中寻找答案的错误,所以我们将不胜感激。

this.addEventListener(MouseEvent.MOUSE_OVER,function() {
        this.alpha=0
        })

在MC中,我希望当鼠标悬停在它上面时它变得不可见。我之所以将此代码放入 MC 中而不是从中创建实例,是因为该 MC 将重复出现多次。经trace() 测试,侦听器确实工作。不管出于什么原因,阿尔法不这样做。感谢您的任何帮助。

This looks like it should work to me, but clearly I've done something wrong. I don't know what exactly and this is apparently to much of a novice mistake to find answers searching Google, so help would be appreciated.

this.addEventListener(MouseEvent.MOUSE_OVER,function() {
        this.alpha=0
        })

In an MC, I want it to become invisible when the mouse hovers over it. The reason I'm putting this code inside the MC and not making an instance out of it is because this MC will recur numerous times. The listener does work, as tested with trace(). For whatever reason, alpha doesn't. Thanks for any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

叫思念不要吵 2024-11-07 09:21:44

当您按原样使用闭包(使用内联匿名函数)时,您会失去范围。 this 不是您将鼠标悬停在其上的对象。

来解决这个问题,

var me:DisplayObject = this;

您可以使用然后(更正的代码)

addEventListener(MouseEvent.MOUSE_OVER,function(event:MouseEvent):void {
    me.alpha=0
})

请注意,我还在您的侦听器中放置了一个 event:MouseEvent 参数,否则您会收到运行时错误(您还没有收到这些错误吗?)

When using closure as you are (using an anonymous function inline) you lose scope. this is not the object that you're hovering over.

You can get around that using

var me:DisplayObject = this;

and then (corrected code)

addEventListener(MouseEvent.MOUSE_OVER,function(event:MouseEvent):void {
    me.alpha=0
})

note that I also put an event:MouseEvent parameter in your listener because otherwise you'd get runtime errors (didn't you get those already?)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文