Passing Parameters to Functions - Looking for good explanation

Just trying to pass a parameter in my event handler to a function.

  1. Why can’t I do this easily anymore
  2. I haven’t been able to find any good threads on the “proper” way to do this in AS

TIA

  1. Someone decided that the idea behind the new event model was better, but you could try something that duncanhall made: http://www.kirupa.com/forum/showthread.php?t=291250

It latches onto the existing event system to add the sort of functionality that I think you want.

  1. As TheCanadian suggests in that thread, the “proper” way could be to make a custom event class and add in special properties to the event that gets passed into the handler. Otherwise, you would have to make sure that some properties of the handler’s enclosing class would be able to give the handler access to the variables that it needs.

someBtn.addEventListener (MouseEvent.MOUSE_DOWN, doSomething);
private function doSomething(e:Event):void {
// i want to have a parameter passed in here from the event handler
}

Which I used to be able to do like this


btn.onPress = function(){
doSomething("example");
}

function doSomething(e:String){
trace(e);
}

I was just reading a post of your but didn’t understand it to be honest.

I edited my first post after (I think) you edited yours, so it has some more info now.

You can do something a lot like that in AS3:

btn.addEventListener(MouseEvent.MOUSE_DOWN, function():void {
	doSomething("example");  
});

function doSomething(e:String){
	trace(e);
}

Thank you ever so much.

:kommie:

You can still do the above now:

someBtn.addEventListener (MouseEvent.MOUSE_DOWN, function (event:Event):void {
     doSomething("example");
});
private function doSomething(msg:String):void {
     // i want to have a parameter passed in here from the event handler
}

Of course you’ve lost access to your listener and would be hard pressed to try and remove it if you needed to.

For more, see:
http://www.actionscript.org/forums/showthread.php3?t=172242#post750911

[edit]Argh :pirate3: beat to the punch while I was searching for the related post :smiley: [/edit]

A notable point in that thread:

also often easier said than done :wink:

I’m a bit skeptical of that claim… or maybe we have different opinions of difficulty. When is it hard to store a reference to a function?

[QUOTE=Krilnon;2355455]I’m a bit skeptical of that claim… or maybe we have different opinions of difficulty. When is it hard to store a reference to a function?[/QUOTE]

in a loop, or when they’re being created dynamically at different times. While it might be easy to actually make the reference, keeping track of it when you need to remove it could be a problem, this especially if you’re making an unknown number of functions for an unknown number of objects or even reactions to an object (or multiple objects).

It seems to me that, in one of those situations, the most complicated part of the code still wouldn’t be storing or finding the function reference, it would be the “making an unknown number of functions for an unknown number of objects or even reactions to an object (or multiple objects)”, or even just figuring out which object you wanted to remove which behavior from that would be the relatively difficult part. If you had that set up and knew the object and the behavior, then it would be simple enough to have some data structure that would take those pieces of information and resolve them into a function reference (because you have those two pieces of info when you create the function anyway).

I don’t really have a good way to assert the ease of doing this other than trying to refute your examples, although I don’t really have a reason to be refuting them in the first place other than that I’ve never run into a situation where it actually is difficult.

Making a class that extends event and giving it one-two params vars seems reasonable to me ; )

Not sure what that’s what duncans class does, haven’t check it.

[QUOTE=sekasi;2355464]Making a class that extends event and giving it one-two params vars seems reasonable to me ; ) [/QUOTE]

The problem with that is that it only pertains to events you have control over and dispatch yourself. You run into a problem when you want to add values to say, a MouseEvent which is dispatched by the Flash Player. There’s no way to tell Flash to dispatch MyCustomMouseEvent.

As for what Krilnon brought up:

[QUOTE=Krilnon;2355461] If you had that set up and knew the object and the behavior, then it would be simple enough to have some data structure that would take those pieces of information and resolve them into a function reference (because you have those two pieces of info when you create the function anyway).[/QUOTE]

At that point you’re not doing the function reference thing, at least not in the context for which it was explained. The basic function reference approach is creating a variable and assigning its value to target the function created. This can be extended to target any number multiple functions dynamically by using something like an array. When you start to get into defining structures to handle this, you’re escaping that whole process and getting into a different approach, one not unlike the Color listener example in the previous link. There objects are created to facilitate the listener and the objects it needs to reference. This works both to be able to have listener parameters as well as provide a “persistent” link to the listener function accessible in the listener call itself through this.listener.

So yea. Basically if you have the objects to store data in, do it. If not, make them yourself. Otherwise you might be able to slide on by with some anonymous functions.

Ah. I was just talking about function references that are either standalone or stuck in an arbitrarily complex structure of other objects. I think that this is pretty much a non-issue then. (-:

Did a blog post on this topic awhile back that might help out with a few different ideas… I didn’t even touch on passing an anonymous function, but I’ve never liked the idea because of the difficulty of removing the listener… There’s 3 other possible solutions though… http://blog.onebyonedesign.com/?p=60