Passing vars with events. or not. My take on how to simply pass vars with events.

I have seen a few posts on the internet about people saying that the hole custom event class is just not there thing or some thing alone that line. In short most of the blog posts focussed on providing different ways to pass a var with an event. I have a simple way to do this as well and it has not been discussed by any one yet so I thought I would put my 2 cents out there.

The idea is that you use the dictionary class to assign vars to an object and then access the var by passing the object to which the var is assigned to as an array index key in the dictionary.

Example:
I this example you would need three movieClips on the stage each with a instance name of someClip1_mc, someClip2_mc and someClip3_mc.

var dictionary:Dictionary = new Dictionary();	

dictionary[someClip1_mc] = "sting1";
dictionary[someClip2_mc] = "sting2";
dictionary[someClip3_mc] = "sting3";

someClip1_mc.addEventListener(MouseEvent.CLICK, traceOut);
someClip2_mc.addEventListener(MouseEvent.CLICK, traceOut);
someClip3_mc.addEventListener(MouseEvent.CLICK, traceOut);	

function traceOut(e:Event):void {
	trace(dictionary[e.target]);
}

Now the pit fall to this solution is that the scope of the dictionary instance needs to be accessible to the event method. Other then that you could use an Object, Array, ect… in the dictionary.


0 Responses to “Passing vars with events. or not. My take on how to simply pass vars with events.”


  1. No Comments

Leave a Reply