-
[RESOLVED] AS3 Any Cons with passing MouseEvents of different types to same handler function?
Hello All,
I was just wondering if there are any problems that could arise from passing MouseEvents of different types (i.e. MouseEvent.CLICK or MouseEvent.MOUSE_DOWN) at different times to the same function?
I've already built my project, and it seems to function fine, I just want to make sure that I'm not setting myself up for some problems in the future. I understand it's probably not the "cleanest" or "best practice" to construct my project in this manner, but if it works...
A simplified example:
PHP Code:
// This is the function that I want to call from varying places with different MouseEvents and triggered by different event listeners
function myFunctionA(evt:MouseEvent):void{
// Do Some Stuff
trace(evt.target.name);
// Do some more stuff, etc.
}
// Here's all my listeners
ButtonA_btn.addEventListener(MouseEvent.CLICK, myFunctionA);
HolderB_mc.ButtonB_btn.addEventListener(MouseEvent.MOUSE_DOWN, myFunctionA);
HolderC_mc.addEventListener(MouseEvent.MOUSE_UP, myFunctionA);
Actually, in writing that, I may have answered my own question, because it seems that myFunctionA is simply expecting a MouseEvent... regardless if that event is CLICK or MOUSE_DOWN or MOUSE_UP event, right?
Now, a MouseEvent.CLICK may have different properties than a MouseEvent.MOUSE_UP that I can access within myFunctionA; however, that shouldn't matter to the function, or is my thinking flawed?
Last edited by badaboom55; 09-22-2009 at 08:39 PM.
-
newb of many sorts
There's no problem with that at all, it's actually meant to be reused. In fact, you can even call the function without a mouse event by passing null as a parameter.
myFunctionA(null);
though, in that case there would be no event to reference.
Search first, asked questions later.
-
half as fun, double the price
If you want ButtonA_btn, HolderB_mc, or HolderC_mc, make sure you use e.currentTarget, not e.target
-
Thanks... I never tried passing NULL before...
That's actually really helpful because there's been times where I've written a handler function that is usually triggered by a MouseEvent... but then the project specs change, and I end up needing to call the same handler function from something completely different... say a frame action (so I can't pass it a MouseEvent reference).
It never occurred to me to send a "null" parameter... I guess because the way Flash requires all the strict data typing, I just figured sending a "null" parameter would get me the same error as sending no parameter at all (or a parameter not of a MouseEvent type)...
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|