BitterSweetJaVa

March 3, 2009

Assign events programmatically in Flex

Filed under: Flex — Tags: , — .|2ic|K @ 4:55 PM

How can you write the line below programmatically?

<local:Item mouseMove="mouseMoveHandler(event)" prop1="value1" prop2="value2" />

Here’s the solution:

var item1:Item = new Item();
item1.prop1 = "value1";
item1.prop2 = "value2";
item1.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);

Assuming that you have the following function:


public function mouseMoveHandler(event:MouseEvent) {
(. . .)
}

If you need to access to an attribute of the Item object inside the mouseMoveHandler function, then you can use the following:


var item:Item = event.target;

event.target will return the corresponding Item object.

Theme: Shocking Blue Green. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.