ActionScript/Flex: Augment MouseEvents with extra information
I've got a business class, Spam
and the corresponding view class, SpamView
.
How can I augment MouseEvent
s coming out of SpamView
so the MouseEvent
s 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 theSpamView
(which isn't very useful)Listening for a
MouseEvent
, creating a newSpamViewMouseEvent
, 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
精彩评论