Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Sending multiple types of variables to a function

Participant ,
Aug 15, 2008 Aug 15, 2008
Hello Community!

I know that it is possible to feed more than one variable to a function -- even if the variables are of different types (the attached code works if I take away all references to the MouseEvent; that is, if I only pass the a, b, and c variables to the function). My question is, can I make this work when I also want to pass a MouseEvent variable to the function as well? In other words, is it possible to place something in the spot where I have the "???" that satisfies the functions demand for an answer to the MouseEvent? I have tried "null", "void", and "undefined" but these do not work.

Any input would be appreciated.

-john
TOPICS
ActionScript
435
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 15, 2008 Aug 15, 2008
To my knowledge, event listeners expect only one parameter - event.

If you need other parameters available to the listener - you need to either use global variables, or target's properties or you may want to write a custom event that will have custom event properties in addition to the native ones.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Aug 15, 2008 Aug 15, 2008
You can do it if you create an anonymous function your event listener responds to.

myThing.addEventListener(MouseEvent.CLICK,
function sendMoreVars(e:MouseEvent){
moreThanOneVar(a,b,c,e)
})


Cheers,
FlashTastic
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 15, 2008 Aug 15, 2008
Yes, it is one of the way of doing it but I think anonymous functions in event listeners are not such a good idea from GC standpoint. How does one remove it? I believe other workarounds are more solid.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Aug 16, 2008 Aug 16, 2008
You are correct Andrei, it would be difficult (if not impossible) to remove this event listener. Thanks for pointing out my mistake.

Cheers,
FlashTastic
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Aug 17, 2008 Aug 17, 2008
Andrei1,

Which other workarounds do you think are better?

Regards,
ropeGun
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 18, 2008 Aug 18, 2008
LATEST
The way you have written your code you don't need to pass additional variables. You declared a, b, and c as global vars in this scope - so they are readily available to the listener. Even if their values will change - listener will capture that. Another story will be if you attach the same listener to multiple buttons and would like to read different variables depending on what button is clicked - here you will need a work around. If this is the case - please tell more about your task.

As it is now, the code should look like this:

var a:String = "a";
var b:uint = 3;
var c:Array = new Array("apples", "oranges", "kiwi");
function moreThanOneVar(me:MouseEvent):void {
trace(a);
trace(b);
trace(c);
trace(me.currentTarget.name);
};
this.myButton.addEventListener(MouseEvent.CLICK, moreThanOneVar);
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines