开发者

Changing alpha of an MC on hover

开发者 https://www.devze.com 2023-02-24 05:32 出处:网络
This looks like it should work to me, but clear开发者_StackOverflow社区ly I\'ve done something wrong. I don\'t know what exactly and this is apparently to much of a novice mistake to find answers sear

This looks like it should work to me, but clear开发者_StackOverflow社区ly 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.


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?)

0

精彩评论

暂无评论...
验证码 换一张
取 消