开发者

ActionScript/Flex: Augment MouseEvents with extra information

开发者 https://www.devze.com 2022-12-23 05:37 出处:网络
I\'ve got a business class, Spam and the corresponding view class, SpamView. How can I augment MouseEvents coming out of SpamView so the MouseEventswhich come out of it contain a reference to the ins

I've got a business class, Spam and the corresponding view class, SpamView.

How can I augment MouseEvents coming out of SpamView so the MouseEvents which come out of it contain a reference to the instance of Spam which the SpamView is displaying?

Here's how I'd like to use it:

class ViewContainer {
    ...
        for each (spam in spams) {
            addChild(new SpamView(spam));
    ...

    function handleMouseMove(event:MouseEvent) {
        if (event is SpamViewMouseEvent)
            trace("The mouse is being moved over spam:", spam)
    }
}

Thanks!

Things I've considered which don't work:

  • Adding event listeners to each SpamView: the 开发者_开发问答book keeping (making sure that they are added/removed properly) is a pain.

  • Using event.target: the event's target may be a child of the SpamView (which isn't very useful)

  • Listening for a MouseEvent, creating a new SpamViewMouseEvent, copying all the fields over, then dispatching that: copying all the fields manually is also a pain.


There are multiple ways to solve this puppy. I would use your #2 option, but build a utility function that gets all of the spamViews on the screen and do a couple of if-elses in looping over your spamViews.

var targ : DisplayObject = DisplayObject(event.target)

If(targ is SpamView) //then you know what's up.

If( loopedSpamView.contains(targ) ) // then the target is inside the spamViewContainer and you should be cool.

Best of luck, Jeremy

0

精彩评论

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

关注公众号