Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Adobe AIR Development

Reply
Developer
DT655
Posts: 71
Registered: ‎04-27-2011
My Device: Bold 9700 and Playbook 64
My Carrier: Rogers
Accepted Solution

Can I pass a second var to the listener function.

new to actionscript, and can't find this on google... I'm sure it exists, but I've worked around it twice, and really want to do this now and don't know how.

 

when adding an event listener

 

 

mybutton.addEventListener(MouseEvent.Mouse_UP, myListener);

the above passes 1 variable of type event.

I catch it as follows:

private function myListener(eventVar:Event):void { //code }

all is good.

I would like to pass more variables to this event listener though.

e.g.
myButton.addEventListener(MouseEvent.Mouse_UP, myListener(what_goes_here, myVariable1:String, myEtc:int));

private function myListener(e:Event, myVar:String, myEtc:int) { }

don't know what is supposed to go in the 'what_goes_here' part of the code above so that it works.

 Any help greatly appreciated.

 

Please use plain text.
Developer
DT655
Posts: 71
Registered: ‎04-27-2011
My Device: Bold 9700 and Playbook 64
My Carrier: Rogers

Re: Can I pass a second var to the listener function.

also,

 

as workarounds,

 

I know I can create a class variable, and pass info that way... so messy,

 

I also know I can   switch(e.target.name) { }    and case each variable to see which button was clicked for instance.

 

I've also been f'ing around with the following:

 

mybutton.addEventListener(MouseEvent.MOUSE_DOWN, function(e:MouseEvent){listenerFunction(e, "string", 3, 4);});

 

Is there not just a way to pass some extra variables?

 

Please use plain text.
Developer
DT655
Posts: 71
Registered: ‎04-27-2011
My Device: Bold 9700 and Playbook 64
My Carrier: Rogers

Re: Can I pass a second var to the listener function.

This is the solution I'm using right now...

 

would still like to know if there is a more elegant solution.

 

 

photosUp.addEventListener(MouseEvent.MOUSE_UP, userClickedFun = function(e:MouseEvent):void { menuClick(e, "showAllPhotos") } );

 

 

ughhhh!

 

dave

Please use plain text.
Developer
TheDarkIn1978
Posts: 409
Registered: ‎12-10-2010
My Device: PlayBook
My Carrier: N/A

Re: Can I pass a second var to the listener function.

[ Edited ]

in my opinion function closures are never elegant.

 

i'm not sure what you are trying to accomplish with your mouse event handler or why you would want to pass something else to that handler.  can you explain what you are trying to do?


PlayBook Applications:
Drop Swatch
Please use plain text.
Developer
RottenOgre
Posts: 274
Registered: ‎11-01-2010
My Device: PlayBook?
My Carrier: WiFi

Re: Can I pass a second var to the listener function.

Please use plain text.
Developer
TongueDar
Posts: 27
Registered: ‎05-05-2011
My Device: Blackberry PlayBook
My Carrier: Not relevant.

Re: Can I pass a second var to the listener function.

I don't know if this will help -- and I've probably been doing it all wrong, but if you build the button in its own function, you can pass in local variables.

 

So something like this;

 

 

function newButton(tBtn:Sprite, varA:String, varB:int):void
{
	tBtn.addEventListener(MouseEvent.MOUSE_UP, btnUp);
	function btnUp(event:MouseEvent):void {	
		// do something with the passed in variables.
	}
}

newButton(TargetSprite, "string", 0);
Please use plain text.
Developer
TheDarkIn1978
Posts: 409
Registered: ‎12-10-2010
My Device: PlayBook
My Carrier: N/A

Re: Can I pass a second var to the listener function.

[ Edited ]

 


TongueDar wrote:

I don't know if this will help -- and I've probably been doing it all wrong, but if you build the button in its own function, you can pass in local variables.

 

So something like this;

 

 

function newButton(tBtn:Sprite, varA:String, varB:int):void
{
	tBtn.addEventListener(MouseEvent.MOUSE_UP, btnUp);
	function btnUp(event:MouseEvent):void {	
		// do something with the passed in variables.
	}
}

newButton(TargetSprite, "string", 0);

 

it's more common to assign a typed variable using a returned function of the same type:

 

for example:

 

 

private var firstButton:Button;
private var secondButton:Button; 

firstButton = createButton("First Button", 50, 25);
secondButton = createButton("Second Button", 80, 40);

private function createButton(label:String, width:int, height:int):Button
     {
     var result:Button = new Button();
     result.label = label;
     result.width = width;
     result.height = height;
     result.addEventListener(MouseEvent.CLICK, mouseEventClickHandler);

     return result;
     }

private function mouseEventClickHandler(evt:MouseEvent):void
     {
     switch (evt.currentTarget)
            {
            case firstButton:    trace("first button was clicked");
                                 break;

            case secondtButton:  trace("second button was clicked");
            }
     }

 

in the example above you could just use evt.currentTarget.label + " was clicked" instead, but it was written just to show how to handle multiple event calls with the same function.

 


PlayBook Applications:
Drop Swatch
Please use plain text.
Developer
TongueDar
Posts: 27
Registered: ‎05-05-2011
My Device: Blackberry PlayBook
My Carrier: Not relevant.

Re: Can I pass a second var to the listener function.

That's what I do for my own stuff. I always use a return function.  I was just putting up a basic exaple.  But then again, I did make the coment about doing it all wrong. =)

Please use plain text.